Efficient Form Validation with jQuery and WordPress

Form validation is a critical part of web development, ensuring that user input is valid before submitting data to the server. In this post, we’ll walk through how to use jQuery for client-side validation and WordPress functions for server-side checks to validate fields like usernames, emails, passwords, and more.

 

Step 1: Include jQuery and jQuery Form Validator
Load jQuery and jQuery Form Validator libraries:

<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<!-- jQuery Validation -->
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>

 

Step 2: Build a Basic Form
Here’s a simple form structure to apply validation to:

<form id="userForm">
  <label for="username">Username:</label>
  <input type="text" name="username" id="username" required pattern="[A-Za-z0-9]+">

  <label for="email">Email:</label>
  <input type="email" name="email" id="email" required>

  <label for="password">Password:</label>
  <input type="password" name="password" id="password" required minlength="8">

  <label for="confirmPassword">Confirm Password:</label>
  <input type="password" name="confirmPassword" id="confirmPassword" required equalTo="#password">

  <button type="submit">Submit</button>
</form>

 

Step 3: Add Validation Rules
Use jQuery to validate the form fields:

<script>
$(document).ready(function() {
  $("#userForm").validate({
    rules: {
      username: {
        required: true,
        remote: {
          url: ajaxurl,
          type: "post",
          data: {
            action: "check_username_exists",
            username: function() {
              return $("#username").val();
            }
          }
        }
      },
      email: {
        required: true,
        email: true,
        remote: {
          url: ajaxurl,
          type: "post",
          data: {
            action: "check_email_exists",
            email: function() {
              return $("#email").val();
            }
          }
        }
      },
      password: {
        required: true,
        minlength: 8
      },
      confirmPassword: {
        required: true,
        equalTo: "#password"
      }
    }
  });
});
</script>

 

Step 4: Custom Validation for Numeric Characters
For a custom validation, prevent usernames from containing numeric values:

<script>
$.validator.addMethod("noNumeric", function(value, element) {
  return this.optional(element) || /^[A-Za-z]+$/i.test(value);
}, "Usernames cannot contain numbers.");
</script>

 

Step 5: Implement Server-Side Validation in WordPress
Add necessary server-side functions to check the availability of the username and email:

// Validate username on server-side
add_action('wp_ajax_check_username_exists', 'check_username_exists');
function check_username_exists() {
  $username = $_POST['username'];
  echo username_exists($username) ? 'false' : 'true';
  exit;
}

// Validate email on server-side
add_action('wp_ajax_check_email_exists', 'check_email_exists');
function check_email_exists() {
  $email = $_POST['email'];
  echo email_exists($email) ? 'false' : 'true';
  exit;
}

 

By combining client-side jQuery validation with WordPress’ server-side checks, you can create robust, secure forms that ensure data integrity and improve user experience. Make sure to test the form to confirm that both client-side and server-side validations are functioning correctly.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.