Nearly every interactive web application requires at some point a user to register. So generally, there exists a registration page where site visitors input basic information like username, password, and email address. Aside from the username and password, what is the easiest way for a PHP web developer to verify an email address is at the very least properly formatted (whether this is an active account is a whole separate matter)?
The approach that come to mind first is to do a simple email string check:
- Make sure the email string contains at most one ‘at’ symbol (@)
- Make sure the email string contains at least one ‘period’ symbol (.) after the ‘at’ symbol (@)
- Make sure the email string contains at least one character before the at symbol (@)
- Make sure the email string contains at least one character between the ‘at’ symbol (@) and the ‘period’ symbol (.)
- Make sure the email string contains at least one character after the ‘period’ symbol (.)
For starters, this is no easy task. Note that the five suggested steps above is not even complete. Many might suggest a complex regex verification process. I have personally attempted to write a regex and even PHP based checker. During this process, I found a great PHP Email Address Validation function available under BSD License which, among others things, allows commercial use.
The function correctly validates on many more rules than the five listed above and has been constantly updated with the changing times. Though “receiving” updates requires downloading a fresh copy of the class, the underlying structure in most cases will remain constant. For a list of the rules that were covered when this script was first created (in 2004), visit addedbytes.com.
Below is detailed step by step instructions starting from the downloading of the code to implementation:
- Go to their official website located at: http://code.google.com/p/php-email-address-validation/ and download the PHP file. As of the writing of this article, the download link can be found by clicking Source > Browse > Trunk. The file EmailAddressValidator.php will then appear in the right panel.
- Copy EmailAddressValidator.php to a file location within your project. In this example, I am assuming the file has been copied to the root of the directory (where presumably your PHP form requiring email validation can be found).
- The next step is to include EmailAddressValidator.php within the PHP form requiring email validation. It is often times best to This can be accomplished with the following code snippet:
include('EmailAddressValidator.php'); - The next step is to to create a new EmailAddressValidator class object. In our example, this object will be called $validator.
$validator = new EmailAddressValidator;
- Finally, we can now use our EmailAddressValidator class object to call the public function check_email_address, which accepts one string input parameter.
$emailAddress = "me@victorchen.info"; $isValid = $validator->check_email_address($emailAddress);
This function returns a boolean value to variable $isValid. It will return true if the email address is correctly formatted and false is the email address is not properly formatted.
That sums up usage of this very convenient and easy to implement class. Happy coding!





Great guide, Victor. I’m sure this will be very useful to anyone wanting to add email validation to their web application.
Yes email validation is a pain when you do not understand how to do so. This should help lots of people.
Hi Victor,
First off, thanks for posting this. I would never have gotten this far without it!
Could you show what the finished explanation looks like? As a total newbie to PHP and Email validation I need all the help I can get!
Thanks,
Bettina
Hey Bettina,
I’m not certain what you mean by “finished explanation”, but the entire script should look something like:
$emailAddress = “me@victorchen.info”;
$isValid = $validator->check_email_address($emailAddress);
Where $isValid will return either true or false. Just replace my email address with the email address you want to verify. So:
if($isValid) {
echo “Is Valid Email”;
} else {
echo “Is Invalid Email”;
}