PHP email validation using the Zend Framework

Posted in PHP -

My last PHP post looked at how to do email validation with filter_var() which is available from PHP 5.2.0. However if the server your website is being hosted on has an earlier version of PHP then you can't use filter_var(). This post looks at how to use the Zend Framework to validate email addresses instead, if you are using the Zend Framework.

Not using the Zend Framework?

If you're not using the Zend Framework in your project then you can still use the email address validator; the beauty of the Zend Framework is you can pick and choose what you want to use. All you need to do is download it and extract it to some location and then add that location to your include path.

For example, I wrote this post using an older version of the Zend Framework which supports a version of PHP < 5.2.0 and saved it to /var/www/ZendFramework-1.6.0 . I then do this to add it to the include path:

ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '/var/www/ZendFramework-1.6.0/library');

And this to include the email validation library:

require_once("Zend/Validate/EmailAddress.php");

Validating an email address with the Zend Framework

Validating an email address with the Zend Framework is as simple as this:

$email = "chris@example.com";

$validator = new Zend_Validate_EmailAddress();
if($validator->isValid($email)) {
    // it's valid so do something
}
else {
   // it's not valid so do something else
}

Additional examples

Using the same examples as in the last post we could do this:

// test good email address
echo $validator->isValid("chris@example.com") ? "good\n" : "bad\n";
// test good email address
echo $validator->isValid("chris@a.b.c.example.com") ? "good\n" : "bad\n";
// not allowed . before @
echo $validator->isValid("chris.@example.com") ? "good\n" : "bad\n";
// not allowed .. in domain part
echo $validator->isValid("chris@example..com") ? "good\n" : "bad\n";
// not allowed . after @
echo $validator->isValid("chris@.example.com") ? "good\n" : "bad\n";
// not allowed double @
echo $validator->isValid("chris@@example.com") ? "good\n" : "bad\n";
// not allowed @ more than once anywhere
echo $validator->isValid("chris@exa@mple.com") ? "good\n" : "bad\n";
// must have @
echo $validator->isValid("chris#example.com") ? "good\n" : "bad\n";

Which would output:

good
good
bad
bad
bad
bad
bad
bad

Finding out reasons for error

If the email address is not valid you can use $validator->getMessages() to get a list of reasons the address is invalid.

For example:

$email = 'chris@@example.com';

$validator = new Zend_Validate_EmailAddress();
if($validator->isValid($email)) {
    // do something
}
else {
    print_r($validator->getMessages());
}

The output from the above is:

Array
(
    [emailAddressDotAtom] => 'chris@' not matched against dot-atom format
    [emailAddressQuotedString] => 'chris@' not matched against quoted-string format
    [emailAddressInvalidLocalPart] => 'chris@' is not a valid local part for email address 'chris@@example.com'
)

You could just as easily loop through the messages using a foreach loop like so:

foreach($validator->getMessages() as $type => $message) {
	echo "$type: $message\n";
}

The output from the above example would be:

emailAddressDotAtom: 'chris@' not matched against dot-atom format
emailAddressQuotedString: 'chris@' not matched against quoted-string format
emailAddressInvalidLocalPart: 'chris@' is not a valid local part for email address 'chris@@example.com'


Related posts:


Comments