News for hackers. Stuff that matters.
Re:The Public Suffix List by dan1234 on Thursday August 18, 2016 @09:01AM (#12312149)
I was thinking of using this as the basis for some email validation as I got a lot of incorrect email addresses in form posts.
Re:The Public Suffix List by throwanem on Thursday August 18, 2016 @10:12AM (#12312574)
Freak_NL says:

> Please don't.

He is correct. Seriously. Don't.

The right way to validate an email address, if that's something you need to do, is by sending it a message with a link and a response code, and seeing whether the user clicks the link or enters the code. This is the only right way. There are many wrong ways. They make people unhappy. They will make you unhappy. Do not use them.

You cannot validate email addresses for shape and form. The sole invariant is that there be at least three characters one of which is @. Everything beyond that is in the hands of the DNS and the domain's MTA. You cannot predict every fashion in which they will behave. You should not try to.

Have you seen the regex that correctly matches every variant of email address form described in RFC 822? It is five kilobytes long. "Ah," you may now think, "I can use that!" You should not. There are new standards with new variations. The regex is incomplete. It will be incomplete forever. It is a five-kilobyte Perl regex. No one will ever understand it well enough to extend it.

Just send a link and a code. Ask the user to click on the link or give you the code. When you have received the click or the code, you know the email is valid. When you have not, assume it is not. This is the method that works. It is the only method that works. Use this method and be happy. Use any other and be sad. Which you prefer is up to you.

Re:The Public Suffix List by snowwolf on Thursday August 18, 2016 @11:32AM (#12313262)
I prefer to do email suggestion rather than validation - The only validation I have is that it should contain an @. And then use something like http://getmailcheck.org/ to suggest spelling corrections for common domains. We saw a massive reduction in bad emails just by implementing this without having to annoy users with incorrectly implemented validation rules.
Re:The Public Suffix List by geofft on Thursday August 18, 2016 @02:30PM (#12315135)
> You cannot validate email addresses for shape and form. The sole invariant is that there be at least three characters one of which is @. Everything beyond that is in the hands of the DNS and the domain's MTA. You cannot predict every fashion in which they will behave. You should not try to.

I used to agree with this viewpoint, but now I'm having some doubts.

Suppose I enter the email address "[email protected]@example.com". It validates according to your rule. Where does it go?

Suppose, furthermore, that I upgrade my servers and they start parsing it differently. They're allowed to do that, right? Maybe my old MTA sent a confirmation email to example.net, and the new one is now sending emails to a user named "[email protected]" at example.com. Haven't I just done something very wrong by allowing a user to trick me into sending emails to an unvalidated address? Isn't this both a violation of the Postel principle ("conservative in what you send to others") and a security hole?

It seems like it would be better for my application to parse the email address and validate what it's doing, and store only unambiguously-parseable addresses.

Or maybe they're not allowed to parse it differently. Maybe there's a single consistent way to parse [email protected]@example.com, and every single email application I might use will get it right. What is this esoteric lore that email applications know that my application cannot? If they can parse it, can't I?

Re:The Public Suffix List by Nadya on Thursday August 18, 2016 @02:40PM (#12315222)
>Suppose I enter the email address "[email protected]@example.com". It validates according to your rule. Where does it go?

Slight word charge on the rule. "..one and only one of which is @..." with characters within quotations ("") not being counted. Quotations need to be considered because this is a valid email address: [0]

`"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com`

>Suppose, furthermore, that I upgrade my servers and they start parsing it differently. They're allowed to do that, right? Maybe my old MTA sent a confirmation email to example.net, and the new one is now sending emails to a user named "[email protected]" at example.com.

If you upgrade your servers and they parse incorrectly by inserting quotations where there were no quotations entered, then the parsing is bugged. Although the validation was bugged to begin with for allowing two delimiters in an email address.

Furthermore, if anyone uses an email that is so heavily eccentric just because it is "technically valid" I'm sure they don't expect their email to work most of the time.

[0] https://en.wikipedia.org/wiki/Email_address

Re:The Public Suffix List by geofft on Thursday August 18, 2016 @02:50PM (#12315279)
Aha. Well, now we're approaching something more complex than /@/, aren't we? :)

I will fully agree with the claim that a regex is the wrong way to validity-check an email, and I will easily believe that implementing the check you describe takes 5 kilobytes of regex. But a rule like "Exactly one un-quoted @ sign" or "Exactly one un-quoted @ sign, and the string on the right needs to be a well-formed domain name" or something is pretty simple in normal code.

"Well-formed domain name" is an existing concept: one or more labels separated by dots, each of which contains only ASCII letters, digits, or hyphens, cannot start or end with a hyphen, and cannot be more than 63 characters long, and no more than 253 characters total. Again, not something I'd do with a regex, but a small number of lines of code.

Re:The Public Suffix List by Nadya on Thursday August 18, 2016 @03:08PM (#12315446)
I'm not the original person you were speaking with - sorry for any confusion.

Although you have me curious how you would check with code without using regex.

Re:The Public Suffix List by throwanem on Thursday August 18, 2016 @02:59PM (#12315358)
> It validates according to your rule.

No. Read the rule again.

Re:The Public Suffix List by geofft on Thursday August 18, 2016 @03:15PM (#12315519)
Did you mean "exactly one of which is @"?

That's not the rule that 'Freak_NL posted, which is why I read your rule as "at least one of which is @".

And, besides, as others have pointed out, "exactly one of which is @" is incorrect. Valid email addresses can have multiple @ characters.

Re:The Public Suffix List by throwanem on Thursday August 18, 2016 @03:29PM (#12315643)
I am not Freak_NL. I am also not interested in participating in the deviancy of people who choose to use email addresses containing quoted strings which themselves contain @s. Anyone who wishes to engage in such self-abuse is entirely free to do so. I don't feel myself constrained to play along.
Re:The Public Suffix List by geofft on Thursday August 18, 2016 @03:35PM (#12315677)
Well, okay, but you've now stepped away from the goal of allowing all valid email addresses, and are now restricting email addresses that don't fit your personal opinion of what a good email address is, which is at odds with what the actual rules for valid email addresses are.

Your opinion might have a lot of reasons to support it, but it does not seem to be consistent with the position you were advocating. I am interested in not validating email addresses, because of your convincing argument that I should not. And now you want me to validate them.

Re:The Public Suffix List by throwanem on Thursday August 18, 2016 @03:44PM (#12315733)
Where did I claim that as my goal? If somebody just has to be a wiseass, that's fine, but I don't mind making him unhappy, because trying to make him happy along with everybody else will make me very unhappy indeed. If I can keep from making myself unhappy, and keep from making unhappy anyone who doesn't just have to be a wiseass, I'm willing to call it a day.
Re:The Public Suffix List by jcranmer on Thursday August 18, 2016 @03:20PM (#12315573)
The Perl regex is also incorrect, since it assumes that comments were stripped beforehand. It also requires you to have backend processing that knows that <a . (kidding) b @ example.com>, <"a.b"@example.com>, and <[email protected]> all refer to the same email address. It would help if people knew that you need to look at RFC 821(/2821/5321) to figure out what constitutes a valid email address instead of RFC 822.
Re:The Public Suffix List by Freak_NL on Thursday August 18, 2016 @10:01AM (#12312502)
Please don't. A lot has been written about email address validation (search HN for a few good discussions); it is horrendously complex to get right. Any mistake or oversight means perfectly valid email addresses won't work, and maintaining your home-grown solution means keeping tabs on any updates to this domain suffix list and other relevant standards.

The most sensible approach (in my opinion) is to validate with this minimal regex:

    /.+@.+/
Essentially just requiring an at-mark between two other bits of text. After that send a confirmation email to see if it works. That is pretty much fool-proof and low maintenance.

If you absolutely must help the user in preventing any common mistakes, use a library that provides hints in the UI for those presumed errors without actually invalidating the input. There are JavaScript libraries that do this fairly well.

Re:The Public Suffix List by brb3 on Thursday August 18, 2016 @10:25AM (#12312683)
> Essentially just requiring an at-mark between two other bits of text.

I've been using this regex to make sure there actually is text before and after the @

    /.+@.+/
Re:The Public Suffix List by infogulch on Thursday August 18, 2016 @11:01AM (#12312978)
Can there be more than one @ or whitespace? If not you could use:

    /^[^@\s]+@[^@\s]+$/
Filtering out emails with multiple @'s and whitespace seems like an obvious win to help prevent typos and copy-paste errors.
Re:The Public Suffix List by wongarsu on Thursday August 18, 2016 @12:12PM (#12313710)
Whitespace in front of the @ is only allowed within quotes, and any quoted part must be seperated by dots from unqoted parts. "my address"@example.com is allowed, as is my." "[email protected], but not my"address"@example.com.

Once you get that into a regex format, remember that a quoted dot is different from an unqoted dot, and don't forget that you handle comments within email addesses correctly.

There are plenty of reasons why people say not to use regex to validate email addresses.

Re:The Public Suffix List by jwilk on Thursday August 18, 2016 @11:30AM (#12313241)
Yes, the are valid e-mail addresses that contain whitespace or multiple @s.

Wikipedia <https://en.wikipedia.org/wiki/Email_address#Valid_email_addr... > gives the following example:

    "very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com
Re:The Public Suffix List by Freak_NL on Thursday August 18, 2016 @01:52PM (#12314765)
Exactly. It is very much our nature as programmers to attempt to capture this seemingly trivial bit of structured text into seemingly innocuous programming rules, but the rewards of the added complexity are just too meager. Even if an email address validates, it still doesn't mean that you have a working email address. Sending an email is the only way to be sure, and if you are already doing that, then why risk excluding valid email addresses with a decidedly non-trivial regex?

There are regexes out there that capture all the complexity of valid email addresses today (but who knows if they'll work with, say, next year's additions to the top-level domains?), and you can copy them from StackOverflow if you really want them; but why bother?

Re:The Public Suffix List by jcranmer on Thursday August 18, 2016 @03:26PM (#12315623)
It is safe in almost all applications to assume that quoted-string local-parts do not constitute a valid email address (ditto for domain literals).
Re:The Public Suffix List by infogulch on Thursday August 18, 2016 @03:06PM (#12315434)
Oh my.

Nevermind. `/.+@.+/` it is.

Re:The Public Suffix List by Freak_NL on Thursday August 18, 2016 @10:26AM (#12312693)
You are right of course. A typo on my end (fixed).
Re:The Public Suffix List by spc476 on Thursday August 18, 2016 @04:00PM (#12315877)
If you feel you really have to validate an email address without sending an email to said address, don't use a regex. Use something that's meant for parsing, like LPeg (for example: https://github.com/spc476/LPeg-Parsers/blob/ee63df57826fd5ef... which handles the RFC grammar, including comments) or lex/yacc or something other than a regex.