Functions:
The functions below are used to validate an email address in PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php //validate email address functions //simple regex function simpleValidEmail( $email ) { $pattern = "/[a-zA-Z0-9_\-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/" ; if (preg_match( $pattern , $email )) return true; else return false; } //advanced regex from PHP source function advancedValidEmail( $email ) { $pattern = '/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD' ; return (bool) preg_match( $pattern , $email ); } //built in PHP function only available in PHP 5.2 or newer //uses the regex found in the above advancedValidEmail function filter_var( $emailaddress , FILTER_VALIDATE_EMAIL); ?> |
The filter_var function shown above can be used to filter and validate many other things that can be seen on the PHP Types of Filters page. The filter_var function function is only available in PHP 5.2 or newer.
NOTE: The regex used in the advancedValidEmail function is from the PHP filter_var source and contains the following copyright from Michael Rushton. As stated: “Feel free to use and redistribute this code. But please keep this copyright notice.”
Example Usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php //example usage for email validation functions $emailaddress = 'test@gmail.com' ; //example email //example usage of the advancedValidEmail function if (advancedValidEmail( $emailaddress )) { echo ( $emailaddress . "is a valid email for advancedValidEmail <br/>" ); } //example usage of the simpleValidEmail function if (simpleValidEmail( $emailaddress )) { echo ( $emailaddress . "is a valid email for simpleValidEmail <br/>" ); } //example usage of the filter_var function if (filter_var( $emailaddress , FILTER_VALIDATE_EMAIL)) { echo ( $emailaddress . "is a valid email for filter_var <br/>" ); } ?> |
Results:
test@gmail.comis a valid email for simpleValidEmail test@gmail.comis a valid email for advancedValidEmail test@gmail.comis a valid email for filter_var