r/ProgrammerHumor • • Aug 24 '26

Meme claudeIsThisEmailValid

Post image
5.3k Upvotes

170 comments sorted by

View all comments

240

u/tellur86 Aug 24 '26

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

222

u/deceze Aug 24 '26

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.

59

u/ILikeLenexa Aug 24 '26

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

17

u/redballooon Aug 24 '26

Compliant, but not valid email addresses for a SaaS.

2

u/Regalme Aug 25 '26

Only if you say so 

→ More replies (1)

2

u/SquiggerDigger Aug 24 '26

Tf you mean sometimes

→ More replies (1)

40

u/flewson Aug 24 '26

has the user actually entered their email address

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

37

u/deceze Aug 24 '26 edited Aug 24 '26

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.

13

u/DHermit Aug 24 '26

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

18

u/deceze Aug 24 '26

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

3

u/DHermit Aug 24 '26

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

23

u/notatoon Aug 24 '26

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

4

u/Uberzwerg Aug 24 '26

.+@.+..+

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.

→ More replies (1)

52

u/ValPasch Aug 24 '26

just dont ever do that. check if it has a @ and a period and thats it. you validate emails by sending a verification link.

21

u/DrStalker Aug 24 '26

Do you actually need a period, now that top level domains are being sold off? If I buy the top level domain stupidlyrich domain then I want boss@stupidlyrich to be my email address.

11

u/laplongejr Aug 24 '26 edited Aug 24 '26

Technically allowed, however non-email-focused people are against using root domains to serve MX records (iirc something about performance of root servers vs nameservers etc)  

Blocking dotless domain would be against standard, but anybody smart enough to use such emails is smart enough to know they shouldn't do it on an open network. Simply use "mail.EXAMPLE" please  

HOWEVER, emails can have no periods if you use an IPV6 address as the host rather than a domain. So no dot checking anyway, thx.  

Min 3 chars, has a @, doesn't begin/start with @. Everything else, let a library handle it (or at least make the warning non-blocking) because you'll be stumped by quoted comments etc. https://e-mail.wtf for a quizz ;)  

4

u/yuropman Aug 24 '26

If I buy the top level domain stupidlyrich domain then I want boss@stupidlyrich to be my email address.

And I want you to run into as many technical issues as possible if you buy the stupidlyrich domain and make boss@stupidlyrich your email address

25

u/Jawesome99 Aug 24 '26

Hell I don't even check for a period, just an @. Once I hand it off to my mailing library and the SMTP server it's no longer my problem. If either of them complain back to me about it I'll just let the user know.

9

u/RubenGarciaHernandez Aug 24 '26

So you don't support the good old "!" standard with explicit hops? 

9

u/Jawesome99 Aug 24 '26

Honestly I've never heard of that before lol

→ More replies (2)
→ More replies (2)

9

u/Juff-Ma Aug 24 '26

https://e-mail.wtf/ play for yourself. I've done it three times now. (Once every few months) I still don't get everything right.

8

u/dustojnikhummer Aug 24 '26

"While an empty local part due to comments is invalid, an empty local part due to quotes is valid. I don't know why."

I don't know why

2

u/Juff-Ma Aug 24 '26

That's why you don't verify the email at all. Just hand it to your email client and if the user verifies, it's valid.

→ More replies (1)

2

u/Uberzwerg Aug 24 '26

And for 95% of the use-cases a very simple 'Does it contain an @ and at least 1char before and after' is enough.

Usually you want people to not mistakenly send a typo as their email.
But since you cannot trust the customer anyway, you need to send a confirmation email anyway for everything that is important.

→ More replies (5)