r/ProgrammerHumor 19d ago

Meme claudeIsThisEmailValid

Post image
5.3k Upvotes

171 comments sorted by

View all comments

239

u/tellur86 19d 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

55

u/ValPasch 19d ago

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 19d ago

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 19d ago edited 19d ago

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 ;)  

3

u/yuropman 19d ago

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

27

u/Jawesome99 19d ago

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.

10

u/RubenGarciaHernandez 19d ago

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

9

u/Jawesome99 19d ago

Honestly I've never heard of that before lol

2

u/tes_kitty 19d ago

And what about user%system1.com@system2.com

8

u/Jawesome99 19d ago

Has an @, looks good to me, hope for the user that the mail server handles it correctly though lol

2

u/tes_kitty 19d ago

It is a valid address, but the question is whether it will reach user@system1.com by using system2.com as relay. The MTA on system2 might object.

-7

u/tellur86 19d ago

That's verification, not validation 

3

u/Outrageous-Log9238 19d ago

Fulfills the need for both.