Email not validating completely with jQuery validation
You can validate email by using jquery validation plugin add method
jQuery.validator.addMethod("validate_email", function(value, element) {
if (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(value)) {
return true;
} else {
return false;
}
}, "Please enter a valid Email.");
$('#checkform').validate({
rules: {
email: {
validate_email: true
},
}
});
Except for testing if there is a value present prior to validating, this is the exact same code posted in the two other answers. In my case, I have an optional email field that I want to validate, but only if an email address is entered.
$.validator.addMethod("OptionalButValidEmail", function (value, element) {
// only test for email address validity if something is entered
if (value.length > 1) {
if (/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(value)) {
return true;
} else {
return false;
}
} else {
return true;
}
}, "Please enter a valid Email.");