The title of this article sounds like it shouldn’t be there, right? Because what’s so hard about validating an email address with RegEx? A lot, as I just learned.
When I googled “regex for email validation” I found examples like this: /[^\s]*@[a-z0-9.-]*/i
That one allows a lot, too much. Even double dots are okay for this expression. More than one dot in a row is neither allowed in the local part, the part in front of the @, nor in the domain part.
Also, by definition, domain parts (the dot and the domain) are limited to 63 characters.
Since all versions I found had flaws and ignored rules, I created my own and want to share the result.
^(?!.*\.{2,})[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])+?(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$
This regular expression avoids double dots, quotation marks, and domain parts longer than 63 characters. It allows subdomains such as “.ca.gov” and all possible characters in the public and the domain part of the email address.
Feel free to play with the regex on regexr.com/74ovj and let me know if you find a scenario that does not work.