Validating Email Addresses – The Daily WTF

Validating Email Addresses – The Daily WTF:

The format for e-mail addresses is specified in a number of RFCs; it’s a pet peeve of mine when people “validate” away perfectly valid addresses, for instance: websites that think all domains end in .com, .net, .edu, or .org; and agents that refuse to transfer mail with a + in the local-part. To that end, I wrote my own regular expression that (I believe) follows the specification, which I’ll share below.

here’s what I use (in perl). there are a few things in the RFC it doesn’t handle, but they’re things nobody seems to really use these days. From my testing, almost no failures of good addresses, and very low rates of bad ones going in (I’d err on the side of letting a bad one through, FWIW, to be caught by a later test like MX resolution).

if (! ($addr =~ /^[a-z0-9\+_\.\-]+@[_a-z0-9\.\-]+\.[a-z0-9]{2,5}$/i)

|| ($addr =~ /@\./)

|| ($addr =~ /@.*\.\./) )

&this_is_bad();

} else {

&this_is_good();

}

You might also want to read:

  1. Avoiding email bankruptcy (part 2) Continuing the discussion from this article… Okay, you’ve blocked out time to handle email. Now what? You need a plan. I’ve tried a bunch of...
  2. Help Create an Email Charter (part 2) In Part 1, I looked at Chris Anderson’s call for an email charter, declared it a failure, and said it ought to be done anyway....
  3. Messaging, not email A VC: Messaging, not email: Coming out of our ‘smtp is dead, long live smtp’ brainstorming session I am thinking that we need to be...
  4. Mark’s Daily Apple » Blog Archive » Wine Prevents Cavities Mark’s Daily Apple » Blog Archive » Wine Prevents Cavities: A new study out today confirms the antibacterial power of both red and white wine....

  • http://jeremy.zawodny.com/blog/ Jeremy Zawodny

    Uh, dude. 1998 called and wants its “&” before Perl function names back! :-)

  • http://profile.typekey.com/dredd/ Derek

    http://search.cpan.org/~pdwarren/Mail-RFC822-Address-0.3/Address.pm
    Don’t reinvent the wheel. CPAN is your friend (and, as it turns out, does it so much more RFC-correct *grin*)