I am trying to validate email only if it is entered.
I started with something like this.
$(function ($) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
$("#Email").blur(function () {
var UserEmail = $(this).val();
if (UserEmail.length > 0) {
if (!emailReg.test(UserEmail)) {
$("#Email").addClass("EmailFormat");
}
else {
alert(UserEmail.length);
$("#Email").removeClass("EmailFormat");
}
}
else {
$("#Email").removeClass("EmailFormat");
}
});
$(document).ready(function () {
$.validator.addClassRules({
EmailFormat: {
required: true,
regExpress: /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/,
messages: { regExpress: "Invalid Email" }
}
});
});
Can I get any help to get this work.
Well I got this worked like this.