Advanced PHP Form Input Validation to Check User Inputs - Validating Email Addresses (
Page 2 of 4 )
Let's start by defining the email address. It contains two sections: the local part and the domain name. For example, in the email address Testuser1@xyz.com, Testuser1 is the local part of the email address while xyz.com is the domain name. Below are the specifications necessary for checking email address validity.
For the local part of the email address, a form should only permit:
- Uppercase and lowercase English letters (a-z, A-Z)
- The digits 0 through 9
- The characters ! # $ % & ' * + - / = ? ^ _ ` { | } ~
- The character . provided that it is not the first nor last character; nor may it appear two or more times consecutively.
(Source: RFC Specification: http://en.wikipedia.org/wiki/E-mail_address )
The domain name half of the email address should be a valid domain. Validating an email address is very challenging, especially if you have a version of PHP below 5, because it requires a long Reg Ex syntax using either the ereg or pregmatch function. Luckily, with the release of PHP 5, you can easily validate email addresses using the filter_var / FILTER_VALIDATE_EMAIL function.
This will automatically check versus RFC specifications stated above. The last thing you need to check to find out if the email address is valid is whether the domain name given is valid. There are lots of approaches to this, including checking to see if the domain name has mail exchange records (MX) or A records.
You can find great resources relating to this task at: http://www.devshed.com/c/a/PHP/Email-Address-Verification-with-PHP/