Here I have 2 email validation regexps, which one is better and why?
new RegExp("^[a-zA-Z0-9_-]+@[a-zA-Z0-9-]{2,}[.][a-zA-Z]{2,}$", "i");
new RegExp("^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4})*$", "");
-
1\$\begingroup\$ Related Email validation using JavaScript \$\endgroup\$Rubén– Rubén2020年07月13日 23:30:25 +00:00Commented Jul 13, 2020 at 23:30
-
\$\begingroup\$ Assuming this is for associating an email to a person, even if the regexp was perfect, that tells you almost nothing about the validity of that person's email. There is only really one way to check a person's email, and that's to email them. \$\endgroup\$Neil– Neil2020年07月14日 00:32:02 +00:00Commented Jul 14, 2020 at 0:32
2 Answers 2
There probably is no 'good' regexp to validate an email id. The Internet Standard RFC has classified a 500-character long RegExp which according to them is the standard way to validate an email-id. Well, it does work, but it is so messy and almost impossible for most of us to understand.
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
If you have understood it, then, you need not read further. But if you haven't, go on.
This page gives a decent info on how to validate an email id using regexp.
I use this one
RegExp("^[A-Z0-9._%+-]+@([A-Z0-9.-]+\.){1,4}[A-Z]{2,4}$","i")
This works fine, atleast for me, but it fails to validate emails on .museum domain or any other domain longer than 4-characters.
Coming to your patterns, both of them are almost same except the fact that the first one uses i
modifier and it allows 2 or more than 2 characters as TLD.
Hope it helps.
-
\$\begingroup\$ The other problem with the RFCs is that they only care about syntactically valid email addresses. [email protected] may be syntactically valid, but it's probably not what the user intended to type, even if the .con domain exists. \$\endgroup\$Brian– Brian2012年12月26日 18:22:00 +00:00Commented Dec 26, 2012 at 18:22
-
\$\begingroup\$ Your suggestion breaks on '@example.com and *@example.com both of which are entirely valid. \$\endgroup\$J99– J992013年07月23日 12:39:53 +00:00Commented Jul 23, 2013 at 12:39
-
\$\begingroup\$ IP addresses are valid server destinations, too. \$\endgroup\$Dave Jarvis– Dave Jarvis2013年07月23日 17:39:44 +00:00Commented Jul 23, 2013 at 17:39
Both are totally broken as they both fail to accept the +
character in the local part and one of them requires more than one digit for the domain name. Single-digit domain names are perfectly fine in some TLDs (such as .de
).
If you really need more validation than a simple "has the *@*.*
format" check, you need to read this answer on Stack Overflow: Using a regular expression to validate an email address