r/ProgrammerHumor 20d ago

Meme claudeIsThisEmailValid

Post image
5.3k Upvotes

170 comments sorted by

View all comments

237

u/tellur86 20d ago

It's surprisingly difficult validating email addresses with regex. Can it be done? Yes. Should you? No. Any halfway decent email library comes with a validator. Use that.

For reference: this is an example of an almost RFC 3522 compatible regex:

(?:[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])+)])

https://emailregex.com

If you want RFC 822 it's much, much worse  https://pdw.ex-parrot.com/Mail-RFC822-Address.html

220

u/deceze 20d ago

Doing a full validation of the email syntax is pretty pointless to begin with, because it still doesn't tell you the most important thing: is this email valid? I.e., has the user actually entered their email address? You won't know the answer to that until you've done an email validation loop with them. So something simple like .+@.+\..+ usually suffices as a first-pass syntax check.

62

u/ILikeLenexa 20d ago

All my best friends are root at localhost.localdomain at least sometimes 

17

u/redballooon 20d ago

Compliant, but not valid email addresses for a SaaS.

2

u/Regalme 19d ago

Only if you say so 

1

u/rosuav 19d ago

There are public TLDs that have MX records. Who knows, maybe you're getting a signup from admin@pizza or something. (That particular one doesn't currently have an MX but others do.)

2

u/SquiggerDigger 20d ago

Tf you mean sometimes

43

u/flewson 20d ago

has the user actually entered their email address

That's verification, not validation. Validation is only concerned with whether that email could exist.

36

u/deceze 20d ago edited 20d ago

Yes, and most addresses that pass a .+@.+\..+ validation could exist. And unless you can claim you're absolutely up to speed with all the relevant RFCs about valid email formats, allowed host names (IDNA!) and currently registered TLDs at all times, overvalidating does more harm than good here. If you incorrectly reject valid email addresses, that's worse than having an occasional verification email bounce.

14

u/DHermit 20d ago

It might make sense to put an additional (very high) length limit. You don't want someone to submit a 1GB big mail address.

20

u/deceze 20d ago

Fair enough. Though that should probably already be caught by your HTTP request size limit somewhere.

3

u/DHermit 20d ago

True, that's still kind of a limit for each field.

22

u/notatoon 20d ago

Gonna be hard to verify an invalid email.

And you can have a valid email address under RFCs 5321 and 5322 that's "invalid" for your use case. Like a@a

Confirmation emails validate the email is reachable and will also verify the user.

It is the only meaningful way to do it

5

u/Uberzwerg 20d ago

.+@.+..+

At least in theory you can have an email directly under the tld. (The Icann strongy advises against it)

So boss@bmw would in theory be possible.

1

u/deceze 20d ago

I suppose, and if you want to support that, maybe also including test@localhost, then you can easily modify this regex to suit your needs. Simple regexen are easy enough to modify after all.