4 ms·
Here's an example regex that implements the official RFC 5322 email validation rules, along with the "preferred" syntax from RFC 1035 (which is one of the recom
by t0mbstone 7y ago
Here's an example regex that implements the official RFC 5322 email validation rules, along with the "preferred" syntax from RFC 1035 (which is one of the recommendations in RFC 5322):
(?:[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])+)\])
Alternately, you can just use this super basic regex which handles all of the common use cases (while still allowing all of the weird edge cases), and functions as a quick "smoke test" for email addresses:
[^@^\s]+@[^@^\s]+
Basically, require at least one character (except a space, a line break, or an @) followed by an @, then at least one character (except a space, a line break, or an @).
The main flaw this simple regex is that it is too permissive and potentially allows weird edge cases like special UTF-8 characters that might not be allowed.
But hey, one might argue that if a user is intentionally entering an invalid email address with wonky characters to create an account, then that's kind of their fault when they never receive the account activation email with the email validation link, right?
- jacquesm 7y agoPrediction: it will fail on many email addresses that are out there in the wild and in daily use. The problem with 99% cases is that if you handle enough accounts that 1% becomes a serious tech support issue. In the end; the best test whether an email address is valid or not is if the mail arrives with the intended recipient. So what you do if an email address does not pass verification is prompt the user to that effect but do not force them to do anything at all.
- mathisonturing 7y agoIs that a prediction for the simple @ validation that was suggested? If so, I'm curious about possible cases
- jacquesm 7y agoGPs comment was edited extensively since I replied.
- t0mbstone 7y agoExactly. The absolute best test for whether or not an email address is valid or not is if the mail arrives to the intended recipient. And if you are going to do that email validation step anyways, you could probably get away with just using the simple highly permissible "smoke test" regex for your email address input form validation.
- jwilk 7y agoYour basic regexp doesn't allow all weird edge cases. For example, this is a valid e-mail address: "f@@bar"@example.org but it's not accepted by the basic regexp. The first regexp doesn't look correct to me either.
- t0mbstone 7y agoOh dang, I didn't even think about @ symbols within quotes. I guess the smoke test regex needs to be even MORE permissive: [^\s]+@[^\s]+ I guess you could get even more crazy and go ahead just do the most bare-bones check possible really: .+@.+ And you were right. The first regex got screwed up when I copied and pasted it. Here's a better source from https://emailregex.com https://emailregex.com (?:[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])+)\]) I suppose there isn't a whole lot of value even using a quick "smoke test" regex, when the big version is already a solved problem, and can be so easily copied and pasted at this point...
- manicdee 7y agoThe only way to validate an email address is to send something to it and either get a reply or have the recipient use the contents to further interact (eg: click a link copy a code). If you are lucky the regex will just cause maintenance issues. If you are unlucky it will stop users signing up.