Validate Email Address

Description: Simple function for checking if an email address is in the proper format.
Tested Platform: PHP 7.1
Language: PHP
// Validate email address and optionally checks validity when local 
// part may contain unicode characters.
// Requires PHP 7.1+ due to use of FILTER_FLAG_EMAIL_UNICODE

function isValidEmail($emailAddress, $unicodeSupported = true) {
    if ($unicodeSupported === true) {
        return (filter_var($emailAddress, FILTER_VALIDATE_EMAIL, FILTER_FLAG_EMAIL_UNICODE) !== false);
    }

    return (filter_var($emailAddress, FILTER_VALIDATE_EMAIL) !== false);
}

Submitted: December 11, 2012

Return to the snippets listing