Home » Php » email validation – Does PHP's filter_var FILTER_VALIDATE_EMAIL actually work?

email validation – Does PHP's filter_var FILTER_VALIDATE_EMAIL actually work?

Posted by: admin April 23, 2020 Leave a comment


After reading various posts I decided not to use REGEX to check if an email is valid and simply use PHP’s inbuilt filter_var function. It seemed to work ok, until it started telling me an email was invalid because I had a number in it.

ie [email protected] works, while [email protected] doesn’t.

Am I missing something or is the filter_var($email, FILTER_VALIDATE_EMAIL) really quite ineffective?

How to&Answers:

The regular expression used in the PHP 5.3.3 filter code is based on Michael Rushton’s blog about Email Address Validation. It does seem to work for the case you mention.

You could also check out some of the options in Comparing E-mail Address Validating Regular Expressions (the regexp currently used in PHP is one of those tested).

Then you could choose a regexp you like better, and use it in a call to preg_match().

Or else you could take the regexp and replace the one in file PHP/ext/filter/logical_filter.c, function php_filter_validate_email(), and rebuild PHP.


[email protected] seems to work fine:

But I’ve definitely seen people complaining it’s got problems, even on SO. In all likelihood, it does have problems, but so will a regex solution. The email address specifications are very, very complicated (RFC XXXX).

That’s why the only solution to verify emails you should rely on is sending an email to the address and demand action (eg: if it’s a registration script ask them to click on a verification link).


that filter has been revamped recently.
http://codepad.org/Lz5m2S2N – appears that in version used by codepad your case is filtered correctly

You can also look at http://bugs.php.net/49576 and http://svn.php.net/viewvc/php/php-src/trunk/ext/filter/logical_filters.c . Regexp is quite scary.


function isValidEmail($email, $checkDNS = false)

    $valid = (
            /* Preference for native version of function */
            function_exists('filter_var') and filter_var($email, FILTER_VALIDATE_EMAIL)
            ) || (
                /* The maximum length of an e-mail address is 320 octets, per RFC 2821. */
                strlen($email) <= 320
                 * The regex below is based on a regex by Michael Rushton.
                 * However, it is not identical. I changed it to only consider routeable
                 * addresses as valid. Michael's regex considers [email protected] a valid address
                 * which conflicts with section 2.3.5 of RFC 5321 which states that:
                 * Only resolvable, fully-qualified domain names (FQDNs) are permitted
                 * when domain names are used in SMTP. In other words, names that can
                 * be resolved to MX RRs or address (i.e., A or AAAA) RRs (as discussed
                 * in Section 5) are permitted, as are CNAME RRs whose targets can be
                 * resolved, in turn, to MX or address RRs. Local nicknames or
                 * unqualified names MUST NOT be used.
                 * This regex does not handle comments and folding whitespace. While
                 * this is technically valid in an email address, these parts aren't
                 * actually part of the address itself.
                and preg_match_all(

    if( $valid )
        if( $checkDNS && ($domain = end(explode('@',$email, 2))) )
            Adding the dot enforces the root.
            The dot is sometimes necessary if you are searching for a fully qualified domain
            which has the same name as a host on your local domain.
            Of course the dot does not alter results that were OK anyway.
            return checkdnsrr($domain . '.', 'MX');
        return true;
    return false;


    var_dump(isValidEmail('[email protected]', true));
    // bool(true)