jump to navigation

Email Validation with ” is_email() ” on PHP November 11, 2011

Posted by Tournas Dimitrios in PHP.

Validating email address , certainly  this should sound very familiar to most Web developers   .  It’s  the process of ensuring that the request is not coming from an automated program that is trying to spam us with bad information . A user could  by mistake  type a wrong character into the contact form  .  Whatever the case , validating a visitor’s email address is  an important step .  It is better to do validation both on the client side and on the server-side. Client side validation provides a quick feedback to your visitor . However , the client side validation can just be bypassed by disabling JavaScript in the browser. Therefore, we need to validate on the server-side as well . 

Validating  an email with PHP could be like this :


// The ereg family of functions, which are gone for good, as they were slower and felt less familiar than the alternative Perl-compatible preg family.
 if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$email)) {

// or
if (!preg_match('/[a-z||0-9]@[a-z||0-9].[a-z]/', $email)){
// or the new PHP5 filter function
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {


A more efficient way for validating email address is to use the is_email() function . This is an email address validation service powered by the free PHP function is_email() created by Dominic Sayers .  Download this function from code.google.com  and start using it . A simple practical example could be :

// Demonstrates the simplest use of is_email()
require_once 'is_email.php';

// Valid
$email = 'dominic@sayers.cc';
if (is_email($email)) echo "$email is a valid email address";

echo '<br/>';

// Invalid
$email = 'dominic.@sayers.cc';
if (is_email($email))
	echo "$email is a valid email address";
	echo "$email is not a valid email address";


Links :



No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s