Email address validation example in PHP

Introduction

This example will show you how to validate email address using PHP programs. The email validation is an important part of the web application when you deal with email address otherwise invalid or bad email address may be stored in your server system.

Email address is an important part of the application because for many situations you need to use email address, for example, you want to build a list of subscribers, you want to provide sign up, password reset page for your end users of the application.

php email validation

Prerequisites

PHP 5.x or higher version

Project Directory

It’s assumed that you have setup PHP in your system.

Now I will create a project root directory called php-email-validation anywhere in your system.

I may not mention the project root directory in subsequent sections, and I will assume that I am talking with respect to the project root directory.

Email Validation in PHP

Here is the PHP script that verifies the email address validation. In PHP 5.2.0 or higher version you can use filter_var($email, FILTER_VALIDATE_EMAIL) function to check whether the input email is valid or not. However in PHP 5.2.0 version the function filter_var() function does not validate for domain or TLD and later it has been fixed in PHP version 5.3.0. So, in PHP 5.2.0 additionally you need to validate for TLD.

function isEmailValid1($email) {
	$pattern = '/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD';

    if (preg_match($pattern, trim($email))) {
        return true;
    }
	
    return false;
}

//filter_var() available in PHP 5.2.0+
function isEmailValid2($email){ 
	return filter_var($email, FILTER_VALIDATE_EMAIL) !== false; //will not validate TLD in PHP 5.2.0
    //return filter_var($email, FILTER_VALIDATE_EMAIL) && preg_match('/@.+\./', $email); //fix for "will not validate TLD in PHP 5.2.0"
}

In the above script I have written two different functions – one is using regular expressions (regex) and the other one is using filter_var() function. If you are using PHP 5.2.0 then you need to use the second return statement in the isEmailValid2() function.

Testing the Email Validation Script

Now I am passing different formats of emails in functions one and two to check how validation behaves.

echo 'Method 1 - isEmailValid1()';
echo "\r\n";
echo "\r\n";

$email = "abc";
$valid = isEmailValid1($email);
echo $valid ? "True" : "False";

echo "\r\n";

$email = "abc@";
$valid = isEmailValid1($email);
echo $valid ? "True" : "False";

echo "\r\n";

$email = "abc@roytuts.com";
$valid = isEmailValid1($email);
echo $valid ? "True" : "False";

echo "\r\n";

$email = "abc@roytuts.com.com";
$valid = isEmailValid1($email);
echo $valid ? "True" : "False";

echo "\r\n";

$email = "bazmega@kapa";
$valid = isEmailValid1($email);
echo $valid ? "True" : "False";

echo "\r\n";
echo "\r\n";
echo 'Method 2 - isEmailValid2()';
echo "\r\n";
echo "\r\n";

$email = "abc";
$valid = isEmailValid2($email);
echo $valid ? "True" : "False";

echo "\r\n";

$email = "abc@";
$valid = isEmailValid2($email);
echo $valid ? "True" : "False";

echo "\r\n";

$email = "abc@roytuts.com";
$valid = isEmailValid2($email);
echo $valid ? "True" : "False";

echo "\r\n";

$email = "abc@roytuts.com.com";
$valid = isEmailValid2($email);
echo $valid ? "True" : "False";

echo "\r\n";

$email = "bazmega@kapa";
$valid = isEmailValid2($email);
echo $valid ? "True" : "False";

echo "\r\n";

The following output is produced when executed using the command: php-email-validation>php php-email-validation.php, where php-email-validation is the project root directory.

Method 1 - isEmailValid1()

False
False
True
True
False

Method 2 - isEmailValid2()

False
False
True
True
False

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *