Coding Cookbook/Validate Email Address

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

[edit] Regular Expressions?

There are many many proposed regular expression solutions to this problem, however the e-mail address format is so complicated that it is impossible to do with a regular expression alone. On the other hand, the vast majority of email addresses that people actually use fall into a small subset of the standard, and thus various usable regular expressions can be found of varying degrees accuracy scattered widely across the internet. If you want to look at a cool regular expression that performs a subset of the email validation process, stare at this for a while; there are also some quick not-quite-perfect solutions here.

[edit] Practical solution

Because parsing an email address is so difficult, it makes little sense to even attempt it; particularly as it would be trivial for someone to give you a correctly formatted email address that still didn't work. This means that the best way to detect whether an email address is valid is to simply send it an email. If you can verify that the email was received, then you know for certain that the email address is valid (though it doesn't guarantee this for any length of time, as many services exist giving out temporary email addresses).

The most common way of doing this is to send someone an email containing a link to an http service with a long random string attached to it, only the email and your server contain the random key, so someone would have to read the email to find the correct link.

[edit] For further reading