How to do email validation in PHP

Every developer at some point of time need to put email validation. Some of the known techniques to validate email address is to use the regular expression as defined in RFC-822 or to follow grammar specified in RFC-5322.

These will be too complicated if you just want a simple and elegant solution to validate an email that covers the basic rules. So lets have look at the two of such solution below in php:

Using PHP in built function filter_var with filter type FILTER_VALIDATE_EMAIL that almost follows the RFC-822

Note: It works only with PHP 5 >= 5.2.0

We can create a Util class for the validation method


Class Util
{
    /**
     * Validates the given string as email
     *
     * @param  string $email Email Address
     * @return bool
     */
    static public function isValidEmail($email)
    {
        return (filter_var($email, FILTER_VALIDATE_EMAIL) !== false) ? true : false ;
    }
}

Since filter_vars returns filtered data on suceess and FALSE if fails, So we should just check that the return is NOT FALSE.

Simple Test to verify


$emails = array (
    "hey+foo@example.com"
    , "test@iana.org"
    , "email@domain.com"
    , "the-local@sld.net"
    , "test@iana.org"
    , "xn--test@iana.org"
    , "john@server.department.company.com"
    , "blah@blah.museum"
    , "hello+world@gmail.com"
    , "_________@domain.com"
    , "01234567890@numbers-in-local.net"
    , "a@single-character-in-local.org"
    , "\"quoted-at-sign@sld.org\"@sld.com"
    , "one-character-third-level@a.example.com"
    , "bracketed-IP-instead-of-domain@[127.0.0.1]"
    , "invalid-ip@127.0.0.1"
    , "test@[255.255.255.256]"
    , "test@[IPv6::1]"
    , "test@[192.168.1.1]"
    , "foo_bar@example.com"
);

foreach ($emails as $email) {
    echo sprintf("%s is %s \n", $email, ((Util::isValidEmail($email) === true) ? 'valid' : 'invalid'));
}


Prior to PHP 5.2, We can use Regular Expression based solution. These regular expressions are very widely available and can be used like one mentioned in RFC- 822


Class Util
{
    /**
     * Validates the given string as email
     *
     * @param  string $email Email Address
     * @return bool
     */
    static public function isValidEmail($email)
    {
        $pattern = "/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}".
            "{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.".
            "(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|".
            "([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i";


        if (preg_match($pattern, $email) === 1) {
            return true; 
        }

        return false;
    }
}

And at last, A very Simple, bare minimum email validation


Class Util
{
    /**
     * Validates the given string as email
     *
     * @param  string $email Email Address
     * @return bool
     */
    static public function isValidEmail($email)
    {
        if (strlen($email) <= 5) {
            return false;
        }

        $pattern = "/^.+@.+\..+$/i";

        if (preg_match($pattern, $email) === 1) {
            return true;
        }

        return false;
    }
}

There are lot more scope to improve the way we validate the emails in application code. Let me know your thoughts and share feedback.

One thought on “How to do email validation in PHP

Leave a Reply

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