Validate an email address in PHP
This article demonstrates how to validate an email address in PHP.
1. Using filter_var() function
The filter_var() function filters a variable using a specified filter and returns the filtered data. You can call filter_var() with the FILTER_VALIDATE_EMAIL filter to validate the email address against the addr-spec syntax in RFC-822.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php if (filter_var($email, FILTER_VALIDATE_EMAIL)) { echo 'Valid email address'; } else { echo 'Invalid email address'; } /* Output: Valid email address */ ?> |
The PHP documentation states that this function will not validate “not latin” domains. Also, it can conflict with RFC-5321 addresses. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $email = 'уникум@из.рф'; if (filter_var($email, FILTER_VALIDATE_EMAIL)) { echo 'Valid email address'; } else { echo 'Invalid email address'; } /* Output: Invalid email address */ ?> |
2. Using preg_match() function
You can also use a regex to validate email addresses using the preg_match() function. The following regex is taken from the FILTER_VALIDATE_EMAIL source. You can use and redistribute this regex, but keep the below copyright notice.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php /* * Copyright © Michael Rushton 2009-10 * http://squiloople.com/ * Feel free to use and redistribute this code. But please keep this copyright notice. * */ $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'; if (preg_match($pattern, $email) === 1) { echo 'Valid email address'; } else { echo 'Invalid email address'; } /* Output: Valid email address */ ?> |
That’s all about validating an email address in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)