HomePHP Page 2 - Advanced PHP Form Input Validation to Check User Inputs
Validating Email Addresses - PHP
PHP form input validation is what separates amateur and professional PHP developers. A professional PHP developer validates data for both security and correctness of the data entered. Keep reading to learn how to validate user input to your forms.
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 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.