Reversed Entropy Can entropy ever be reversed?

26Jun/100

Validating email addresses

Email address validation is a common issue in any language, but it's more common in those oriented to create web pages like PHP.

The standar for email address naming is the RFC 2882, and it's long, long and complicated. Too complicated to fit in a regular expression, so I don't recommend anybody to try a regex implementation of the full standar, it's simply impossible.

The best approach to solve this particular problem is to write a small and understandable regular expression that accepts some invalid email addresses but catches all the valid ones. Is better to let some inexistent emails than don't let an user register with his completely valid and working email.

Double check: one using regex and once passed validate the email address on the only way that you'll now that is valid for sure: send an email containing an activation link.

Since we are going to double check is OK to trust the client and do the first validation using JavaScript. Once passed  we'll send an email to that address on the server side, and it won't be confirmed until the activation link is clicked, so as I said before: is OK if you get some invalid email address.

Keeping it simple: I only check for something followed for a @ then another something with a dot and a final something (with any characters and even more dots!).

/((.+)@(.+)\.(.+))/

The above regular expression will:

Match

  • user@mail.com
  • user+mail@mail.com
  • user+mail@mail.co.uk
  • mail@mail.commm
  • user/mail@gmail.com
Not Match
  • user+mail@.com
  • @mail.com
  • user@mail

As said, it will match invalid email addresses, just confirm them sending an email, the rest is just meaningless effort to solve a problem that cannot be solved using regular expressions.