3 ms·
The HTML <input type="email"> regex is a good starting point for "what's a valid email address": [1] /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-z
by jcranmer 4y ago
The HTML <input type="email"> regex is a good starting point for "what's a valid email address": [1]
/^[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])?)*$/
If you want to be a little more precise, the '.' characters in the local part cannot be consecutive nor occur at the start or end of the string. Also, the syntax does not account for EAI (add any non-C1 control non-ASCII Unicode character to the list of allowable characters), nor does it account for IDN (complicated, but it's IDN, there's libraries for it).
Unfortunately, email is in a weird space where there's a lot of clearly incorrect validators out there that fail on common stuff (e.g., dashes in domain names, weird TLDs, etc.), but trying to find a good one is hard because many people want to "help" by flexing how good they are at covering really, really weird cases that you honestly shouldn't want to support, like relay routing, or RFC 822 comments in email addresses (which aren't actually part of the email address!).
[1] https://html.spec.whatwg.org/multipage/input.html#email-state-(type=email) https://html.spec.whatwg.org/multipage/input.html#email-stat...
- hvdijk 4y agoExcept that the HTML spec explicitly states just before that regex that what it checks is not whether it's a valid e-mail address, and that it intentionally rejects valid e-mail address.
- IIsi50MHz 4y agoIndeed. <local portion>@<destination portion> is how I seem to remember "email address validation". The '@' is the only reserved visible character. The local portion is ONLY to be interpreted by the destination host. Destination portion does not have to follow any pattern except to specify something (such as a path or resource name) findable by the mail server. Of course, if you specify a destination only understood by your local email server as a destination in your LAN/WAN/whatever when talking to somebody/something outside that, they won't be able to reach you. Addresses tend to look a lot more orderly than they used to, but I think this would be a "valid" address (within a given system): 12855977.233~foo+esr@bar!baz.crunchly!test%test#test\\splat\3