Live data from Hacker News

Stop Validating Email Addresses With Your Complex Regex

davidcel.is

151–160 of 211 posts

Re: Stop Validating Email Addresses With Your Complex Regex

#152
post #135
post #66

Earlier quoted context omitted.

> This puts the burden of checking email validity on every user, even perfectly capable valid users. ? Whenever I hit a form which wants me to retype my address, I just triple-click to select the entire address, then middle-click to paste it into the confirmation field.

Meet the airline I booked with yesterday: two email fields, with paste disallowed only on the second one . :(

How do you even prevent that? You need some seriously broken code to catch a system wide short-cut.

Re: Stop Validating Email Addresses With Your Complex Regex

#153

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.

If using IPs in an email address you are supposed to surround it with square brackets anyway.

I wouldn't add IP address support to an email regex because I'd rather turn away such perverse data anyway. Nobody uses IP-based email addresses.

Re: Stop Validating Email Addresses With Your Complex Regex

#154

Earlier quoted context omitted.

to ensure it is deliverable? Well, then you better send them an email. I deal with user support for a site and I'd estimate at least 2% of our new users (>50 people PER DAY) enter wrong email addresses. Not "I forgot to put .com at the end" but "I thought my email was john.doe@gmail.com when it's actually john.doe@yahoo.com" which would pass validation with flying colours. The only real "solution" is to tell a user i…

>if it has and they don't have it allow them to change their email to their real email address Couldn't an unscrupulous individual use that feature to take over non-activated accounts? An immediate use for that exploit doesn't spring to mind but this makes my spidey sense tingle. What sites allow this?

From my experience a user will almost always remember the password they've just entered, even if they got the email wrong. They should be able to login to a not-yet activated account and be presented with the option to correct the email for the activation email to be sent to. There's no potential for abuse there.

Re: Stop Validating Email Addresses With Your Complex Regex

#155
post #135
post #66

Earlier quoted context omitted.

> This puts the burden of checking email validity on every user, even perfectly capable valid users. ? Whenever I hit a form which wants me to retype my address, I just triple-click to select the entire address, then middle-click to paste it into the confirmation field.

Meet the airline I booked with yesterday: two email fields, with paste disallowed only on the second one . :(

Yet another reason I love lastpass. None of this nonsense anymore.

Re: Stop Validating Email Addresses With Your Complex Regex

#156
post #79

Earlier quoted context omitted.

Because most users couldn't type their own email address, or even a properly formatted email address to save their life. "My email address is joe.aol or was it aol.com@joe? Wait joeaol@com?"

I'd say nothing of value is lost in that case. These people are very costly to support. Email has been in common use for at least 20 years. They need to step up to the plate and learn at this point.

I agree. In addition to being pretty damn old at this point the pronunciation of "@" should resolve the "aol.com@joe/joe@aol.com" issue for all but the most clueless (all but the most likely to cost you in support). That case is particularly egregious.

Re: Stop Validating Email Addresses With Your Complex Regex

#157

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.

If using IPs in an email address you are supposed to surround it with square brackets anyway. I wouldn't add IP address support to an email regex because I'd rather turn away such perverse data anyway. Nobody uses IP-based email addresses.

A square bracket, however, is not a dot (which is for what the original regexp checked).

And I just tried sending an email to me@[my.ip.addr.[0]]. Postfix somehow recognised it was for this local host, but failed because it wasn’t part of the virtual domains I had put into it. ‘perverse’ seems to be somewhat appropriate.

[0] Sorry, too lazy to check the appropriate documentation IP address ranges. 20db?

Re: Stop Validating Email Addresses With Your Complex Regex

#158

Assuming that running the regex is much faster than sending an email, it would probably be much less server load to check the regex and never send X% of emails, unless X is extremely small. (Looking up and implementing a regex) * 1 + (running the regex) * (every email) + (sending email) * (every valid email) Also, this post only considers the signup/activation use case. If you're getting an email for ecommerce to sen…

This assumes that you get the regex 100% right and never lose a user by rejecting a valid email address. This is much harder than it seems ( http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html ), and is no guarantee an valid email address that is in use, as the article makes clear. After some very basic checks, e.g. "contains at at least 3 chars, one of which is an @", you should Just. Send. The. Email. Who bother…

Please don't quote that RFC-822 regexp when arguing this. That's for the contents of mail headers (which can include comments and so on), not an actual valid email address.

A regexp for validating RFC-2821 email addresses is actually fairly simple.

Re: Stop Validating Email Addresses With Your Complex Regex

#159
This has come up so often on Hacker News that I decided to create a very simple JSON API for checking email addresses. Free to use for anyone. Performs the right regexp check for email addresses based on RFC-5321 rules (not the oft-quoted but incorrect RFC-822 rules, which are for mail headers), performs MX lookups to ensure mail can be delivered, and performs the same "did you mean" type checks that kicksend's mailcheck performs.

I've included both jQuery and server side example code on the site.

https://www.emailitin.com/email_validator

Re: Stop Validating Email Addresses With Your Complex Regex

#160

So what's wrong if you do a full validation ( http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html ) ? You as developer or site owner or user don't need to do it by hand or in your head. It is done in a fraction of a second by the computer even if benefits are not the greatest like validating the strength of a password but still. Complaining about it because you don't like it and telling other people not to do it be…

There's nothing wrong with doing full validation. Just don't use that regexp - it's for RFC822 email addresses, which is how you might see them in an email header, including things like comments.

You want an RFC821 (or more specifically RFC5321 now) email address regexp. See my post here about the email validator I wrote: https://www.emailitin.com/email_validator

Post reply on HN