Nope, that regex is not that fit for the purpose. Have a look at this instead (though I can't guarantee it's validity).
Also, regarding the script itself, why are you not checking like this:
function checkEmailValidity(e) {
return e.match("some validating regex");
}
It seems like a faster, more consise, and more readable solution.
EDIT:
It's worth noting, that it's almost impossible to write a regex that can detect any valid email address. Therefore, you might be better off with trying to make some validation algorithm, instead of a regex, as valid email adresses may be very, very complex.
Consider this code:
function validateEmail(email) {
if (typeof email != "string") return false;
email = email.split("@");
email[1] = email[1].split(".");
if (
email.length !== 2 &&
email[1].length < 2 &&
!email[1].hasValues(String)
) return false;
return true;
}
// Checks whether each key in an array has a value, and (optionally) if they match a given contructor (object type).
// I.e.: myArray.hasValues(String) // checks whether all elements in myArray has a value, a nonempty string.
Array.prototype.hasValues = function(assumedConstructor) {
for (var i = 0; i < this.length; i++) {
if (!this[i]) return false;
if (assumedConstructor && this[i].constructor != assumedConstructor) return false;
}
return true;
};
It works this way:
- First checking if the string contains one
@
, and only one
- Checks that the part after the
@
has at least one .
- Checks that there is some characters between each of the possible
.
's.
It will still be easy to forge a fake email address, but this way we ensure that it's at least somehow properly formatted. The only gotcha that I can think of, is @
's inside comments, which should be perfectly legal according to the RFC's, but this code will treat it as an error.
Normal internet users, which have normal email adresses, will not fail this. So how much it actually matters is up to you to decide ;)
The best solution, if one is available, is to put the address into some built-in method, that somehow checks for validity by trying to use the email address.