This simple PHP function below will check to see if an email address is valid format/syntax or not by using PHP Regular Expression. It’s recommended to validate and check all input fields before inserting into the database especially with email address.
The function below will preg_match() PHP built in function to validate the format/systax of a email address by encoding its format in a regular expression. It looks for matches to the email pattern and will return true if matches are found, otherwise will return false.
PHP function to validate email address
<?php function checkEmailAddress($email) { if(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $email)) return true; return false; } ?> |
Usage
<?php $email = "[email protected]"; if(checkEmailAddress($email)) echo $email . " is a valid email address!"; else echo $email . " is not a valid email address!"; ?> |
Download the php-validate-email-address