No, this regular expression is not suitable for this purpose. Look at this instead (although I cannot guarantee its authenticity).
Also, as for the script itself, why don't you test it like this:
function checkEmailValidity(e) { return e.match("some validating regex"); }
This seems like a faster, more understandable, and more readable solution.
EDIT:
It is worth noting that it is almost impossible to write a regular expression that any valid email address can detect. Therefore, you might be better off trying to do some validation algorithm rather than a regular expression, since valid email addresses can 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). // Ie: 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 as follows:
- First check if the string contains one
@ and only one - Checks that the part after
@ has at least one . - Checks if there are any characters between all possible
. .
It will still be easy to fake a fake email address, but in this way we guarantee that it is at least somehow formatted. The only thing I can think of is @ internal comments, which should be completely legal according to the RFC, but this code will treat it as an error.
Ordinary internet users with regular email addresses will not let you down. So, how important this is, it's up to you to decide;)
The best solution, if available, is to put the address in some kind of built-in method that somehow validates by trying to use the email address.
source share