Live data from Hacker News

Stop Validating Email Addresses With Your Complex Regex

davidcel.is

51–60 of 211 posts

Re: Stop Validating Email Addresses With Your Complex Regex

#51
post #34

The question is why people are validating the email in the first place. * to ensure it is deliverable? Well, then you better send them an email. * to let people know when they misread the labels and put something that was clearly not an email in the email field? A simple check for an at-sign is usually sufficient. * because some tester opens a ticket saying you can enter an invalid email in the email field? Yeah, tha…

Testers gotta find bugs. QA's needs eat too you know.

Re: Stop Validating Email Addresses With Your Complex Regex

#53

For those that use PHP, use the following: filter_var($email, FILTER_VALIDATE_EMAIL); Regex from the source: https://github.com/php/php-src/blob/master/ext/filter/logica...

yes, there's a good page here about the different regexs:

http://fightingforalostcause.net/misc/2006/compare-email-reg...

Re: Stop Validating Email Addresses With Your Complex Regex

#54

If you really want to do checking of email addresses right on the signup page, include a confirmation field so they have to type it twice. No. This puts the burden of checking email validity on every user , even perfectly capable valid users. If you're validating for edge cases (mistakes or otherwise invalid addresses), treat it as an edge case and don't annoy users who can type.

Perhaps every sign up page should include a "I just want to check it out" button to let you in and demo the product.

Re: Stop Validating Email Addresses With Your Complex Regex

#55
A basic regex is more than enough and you don't even need to deliver a message, just connect to the MX for their domain and check that A) The domain resolves an MX and B) That you can handshake for a 250 OK on the rcpt to header only, then drop the socket. Done! It's not that slow and you're leveraging the one thing an SMTP server does really well - be RFC822 compliant. It's something that can be delegated out of process anyway (as a promise or RPC etc) as soon as the email is entered, and resolved when they submit the form. Problem with the email? Then raise it for correction or pass-through... its probably the same amount of code and half the time for end-to-end delivery testing than crafting a bunch of edge case regexes and praying it works.

Re: Stop Validating Email Addresses With Your Complex Regex

#56
post #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 e…

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

A valid email address can contain almost anything; this makes validation via a standard parser mostly useless. As such, devlopers reach for stricter parsers out of a combination of a not comprehending the standards, feeling vague discomfort about letting 'just anything' past data validation, and misplaced concern for users that they believe can't type their own e-mail address.

Add to that the occasional business complaint from the marketing arm about bogus e-mail addresses, and you have people repeatedly solving the problem in slightly different ways, justifying their own divergences from the standard by applying the justification that nobody will use a 'weird' address anyway, and they're actually being helpful.

Re: Stop Validating Email Addresses With Your Complex Regex

#57
post #34

The question is why people are validating the email in the first place. * to ensure it is deliverable? Well, then you better send them an email. * to let people know when they misread the labels and put something that was clearly not an email in the email field? A simple check for an at-sign is usually sufficient. * because some tester opens a ticket saying you can enter an invalid email in the email field? Yeah, tha…

* Because users often miss a character like a dot or an @, and catching that early saves a lot of pain with undelivered confirmation e-mails and so on.

Re: Stop Validating Email Addresses With Your Complex Regex

#58

Don't bother even reading it. His solution is to "Just send your users an email. The activation email is a practice that’s been in use for years, but it’s often paired with complex validations that the email is formatted correctly. If you’re going to send an activation email to users, why bother using a gigantic regular expression?" Want to know why it's not more common than the regex "method"? His method has its own…

""If a user can't get their email address entered correctly, I don't want them as a customer""

Kinda harsh sentiment considering that we all mistype stuff, especially on site that disable auto complete.

Re: Stop Validating Email Addresses With Your Complex Regex

#59

Earlier quoted context omitted.

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.

IPv4 also has a valid decimal representation. http://1249764136/ will send you to Google!

Re: Stop Validating Email Addresses With Your Complex Regex

#60
post #6

I would rather lose a few users through a faulty regexp than lose double digit percentage through an email activation step.

Think about the repercussions of not using a validation email. Anyone can sign anyone else up. That's even more unacceptable IMHO.
Post reply on HN