3 ms·
If you think names are hard, try email addresses. Most complex validator I ever wrote, and then had to switch off lots of it because email addresses in the wild
by jacquesm 7y ago
If you think names are hard, try email addresses. Most complex validator I ever wrote, and then had to switch off lots of it because email addresses in the wild don't actually follow the RFC...
In school we had a guy whose last name was 'Van', a fairly common prefix for names in NL, which tends to be used like this 'van den Broek' or 'van der Berg'.
Teachers would ask for his name, he'd say 'Jan Van' and they'd invariably ask 'Jan van Wat?'. So he eventually gave up and answered 'Jan van Wat' right of the bat, which would leave the teachers even more confused because there was no such person on their lists of students...
Given that we know at least one guy whose first name is Van (Morrison) it is theoretically possible there is a person called Van Van somewhere.
- Piskvorrr 7y agoNope. Email validation is easy: contains an "@", and gets delivered somewhere. Everything else is too clever by half. Names lack this interactive verification...and oh my goodness, the tussenvoegsel thing you mention: the horror...the horror!!! (Oh, you wanted to sort the names? That's a Lovecraftian nightmare of its own.)
- sethhochberg 7y agoDuring registration flows, I'm also a fan of validating that the domain has MX records published - DNS queries are a pretty cheap option for detecting whether its at least plausible that your validation email will arrive later. At scale, its an easy option for helping users detect typos in their email domain (or for helping guide less technical users, like those who might provide address@gmail or similar instead of address@gmail.com)
- 9nGQluzmnq3M 7y agoIf you try to make @ mandatory, you'll soon get an angry mail from hn!verizon!randomisp!somehipster complaining how you're marginalizing people who still use bang paths. https://en.wikipedia.org/wiki/UUCP https://en.wikipedia.org/wiki/UUCP
- Piskvorrr 7y agoSorry about that, then. I also have no gopher support and redirect all HTTP to that newfangled HTTPS - so such person would not even get to such a form in the first place ;)
- t0mbstone 7y agoHere's an example regex that implements the official RFC 5322 email validation rules, along with the "preferred" syntax from RFC 1035 (which is one of the recommendations in RFC 5322): (?:[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])+)\]) Alternately, you can just use this super basic regex which handles all of the common use cases (while still allowing all of the weird edge cases), and functions as a quick "smoke test" for email addresses: [^@^\s]+@[^@^\s]+ Basically, require at least one character (except a space, a line break, or an @) followed by an @, then at least one character (except a space, a line break, or an @). The main flaw this simple regex is that it is too permissive and potentially allows weird edge cases like special UTF-8 characters that might not be allowed. But hey, one might argue that if a user is intentionally entering an invalid email address with wonky characters to create an account, then that's kind of their fault when they never receive the account activation email with the email validation link, right?
- jacquesm 7y agoPrediction: it will fail on many email addresses that are out there in the wild and in daily use. The problem with 99% cases is that if you handle enough accounts that 1% becomes a serious tech support issue. In the end; the best test whether an email address is valid or not is if the mail arrives with the intended recipient. So what you do if an email address does not pass verification is prompt the user to that effect but do not force them to do anything at all.
- mathisonturing 7y agoIs that a prediction for the simple @ validation that was suggested? If so, I'm curious about possible cases
- jacquesm 7y ago
- jwilk 7y agoDo you have an example of an in-the-wild email address that doesn't follow the RFC?
- jacquesm 7y agoThis project ran 5 years ago, sorry I do not have access to that data anymore. But it wasn't just a few. The big providers got it right but there are so many smaller ones with their own tweaked mail servers, those were the ones causing the problems.