<input type="text" id="email" name="email" class="required email" />
, in this code, values for the class attribute 'required' and 'email' act same as the rules define in validate method in document ready function. That means class="required email"
equals to,
email: {
required: true,
email: true
}
in above example.
I will tell what are the steps needed for including jquery validation. (you may already know each and every step)
For use jquery validtion,
First you have to include main jquery library js file and jquery validation plugin js file.
Then on the document on ready function, you have to bind validate function to required form ( as mentioned in the example of above post).
You can either declare validation rules in this validate function or as values for class attribute of the relevant input element as I have described above.
Sometimes I have got problems when I use different values for id attribute and name attribute for input elements. I don't know the reason for it. So try to give the same values for id and name attributes and check if still validation not works.
You can use already included validation rules such as email, url, number, digit etc.
and also there is a rule called "remote" which can use for sending ajax request and do validation in server side.
In addition to that you can define custom rules according to your needs.
For example, I used following code to add custom validation rule for ensuring passwords are in correct password policy.
$.validator.addMethod("passwd_policy", function( value, element, param ) {
return this.optional(element)
|| (value.length >= 8
&& /.[!,@,#,$,%,^,&,*,?,_,~]/.test(value)
&& /[0-9]/.test(value)
&& /[a-z]/.test(value)
&& /[A-Z]/.test(value));
},"Your password must be at least 8 characters long, <br/>contain at least one number, <br/>"
+" at least one special character (!,@,#,$,%,^,&,*,?,_ ,~),<br/> at least one uppercase"
+" character <br/>and at least one lowercase character.");
You can add this custom validation method to validate a field as same as using inbuilt methods. that is like
"txt_passwd": {
passwd_policy: true
},