How can I allow only gmail.com as domain in my email field validating using jquery-validation plugin ?
$(\"#myForm\").validate({
rules: {
myemail: {
Take the Gmail regex from this answer and use it within the addMethod method to create a new custom rule called "gmail"...
jQuery.validator.addMethod('gmail', function (value, element) {
return this.optional(element) || /^[a-z0-9](\.?[a-z0-9]){5,}@gmail\.com$/i.test(value);
}, "not a valid Gmail email address");
$("#myForm").validate({
rules: {
myemail: {
required: true,
gmail: true
}
}
});
It does not matter if you call .validate() before or after the .addMethod() method.
DEMO: http://jsfiddle.net/fPFpF/