I am using a jQuery validator to validate that a proper email address has been entered into a text field. However, it seems that there are times when this validator is not being triggered.
Scenario: A user enters in their email "user.email.provider.com". As they're typing, the validator is consistently telling them that the email is not in the right format. They highlight the period between "email" and "provider" and change it to "@". While the email (user.email@provider.com) is in the right format, the error remains. If they then hit a space at the end of the email - or even in the middle of the email (user.email @provider.com) - the error goes away.
Here is the code that adds the validator and uses the REGEX:
self.initialize = function () {
$.validator.addMethod("emailWithDomain", function (value, element) {
return this.optional(element) || /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i.test(value);
}, "Enter an Email Address in a valid email address format.");
$("#pageForm").validate({ ignore: [] });
$.each(self.rows(), function (index, key) {
self[key.name] = key;
});
};
self.initialize();
I believe that it gets applied to the element here:
self.render = function () {
switch (self.id()) {
case 'first-name':
case 'middle-name':
case 'last-name':
case 'suffix':
self.addLengthRequirement();
break;
case 'email':
self.addLengthRequirement();
self.selector().rules('add', {
emailWithDomain: true
});
break;
}
};
The selector is as follows:
self.selector = function () {
return $('input[name=' + self.name() + ']');
};
It seems that the only time the validation happens is when the length of the value changes, not when a key is pressed or key change happens.
I am not able to provide a complete code snippet. It is my hope that this information is enough that someone can help point out the issue. I'm not familiar with jQuery validators nor REGEX enough to know exactly what the issue is, but I have a reported bug and I believe this is where the issue lies. If anyone is able to help, I truly appreciate it. Thanks.