Live data from Hacker News

Stop Validating Email Addresses With Your Complex Regex

davidcel.is

21–30 of 211 posts

Re: Stop Validating Email Addresses With Your Complex Regex

#24

My goto for email validation is /^.+?@.+?\..+?$/ Incase I've typed it wrong, that should basically work for anything that contains at least one @ and one dot, in that order, as well as at least one character at beginning, middle and end. It's served me well thusfar. Edit for clarification: The reason I prefer this over just checking for an @ is that if you're just checking for @ a common mistake like "me@hotmail,com"…

I think that the domain part of email addresses could be an IP address. Depending on how IPv6 addresses are displayed there, they won’t contain a dot. Somewhat artificial, yes.

I'll admit I hadn't considered IPv6b addresses, might have to rethink my trusty regex. Sad, it's served me well for so many years.

Re: Stop Validating Email Addresses With Your Complex Regex

#25

My goto for email validation is /^.+?@.+?\..+?$/ Incase I've typed it wrong, that should basically work for anything that contains at least one @ and one dot, in that order, as well as at least one character at beginning, middle and end. It's served me well thusfar. Edit for clarification: The reason I prefer this over just checking for an @ is that if you're just checking for @ a common mistake like "me@hotmail,com"…

I thought new TLD being worked on didn't need to have dots in them. Why not just check for x@x?

That's exactly what you should do.

^(.+)@(.+)$

max length is 254 according to the RFC I believe, so you can check for that too.

Re: Stop Validating Email Addresses With Your Complex Regex

#27
post #7

"Feeling ambitious? Then check for the dot too: /.+@.+\..+/i. Anything more is overkill." I personally prefer: /[^@]+@[^@]+\.[^@]+/ Basically the same except that it will throw an error if someone enters an extra at sign.

Yeah, I basically check for one @ sign and some very basic sanity checking on the right side.

/^[^@]+@[^@]+\.[^@.]+$/

Re: Stop Validating Email Addresses With Your Complex Regex

#30

This has been an issue since the day I started programming for the web, back somewhere in '95. It has regularly come up on HN, and pretty much any programming related forum I've used since the mid-90's. As an industry at the heart of the information society you have to wonder what the hell we are doing wrong if we cannot stop this constant regression into well known bad practices.

I understand the argument re validating email addresses passively (regex, no regex, etc.) vs actively (send an email by SMTP).

What I don't understand with this ever-repeating discussion is why the complexity has to be visible. e.g.

    > 
    > Yeesh. Is something that complex really necessary?
Many functions are complex - we put those in libraries, pushing them under the hood, and move on.

What is so special about parsing email addresses that makes everyone invent their own solution - regex or otherwise?

Post reply on HN