This article discusses client-side validation using jQuery’s validation plugin. This jQuery plugin has a bunch of standard validation methods such as a required field, valid email or URL, minimum and maximum lengths for a field, etc. In addition to that you can customize the look and feel of the error messages and their placement.
All we are doing here is initializing the validation of the form using the validate() function. It can take several parameters. In the example above, we use only two of them, but you can find a list of all the options for the validate() function at Validate options.
The two parameters we used in validate method are:
rules: allows you to specify which fields you want to validate. messages: allows you to specify the error message for a particular field. If you don’t specify it, a default message is provided that says “this field is required”.
In the example above, we only used three validation methods (required, minlength and maxlength). I’ve also introduced the format method in the message. This lets us replace the argument given for minlength and maxlength, such that we don’t need to hard-code that value if they ever change.
There are several other methods that can be used here. You can find list of built-in validation methods at Validation Methods.
Custom Validation Rules:
Suppose if you cant find any suitable built-in method for certain situation, then you can write your own validation methods. For example, there is no built-in method to validate Alpha character, we need to define our own method.