In this article, different methods to validate an email address in PHP.It uses regular expressions and an inbuilt email validation function in PHP.
Using: regular function preg_match() validates an e-mail address:
1 2 3 4 5 6 7 8 9 10 11 |
<?php function isValidEmail($str) { return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE; } if(!isValidEmail("info@techarise.com")){ echo("This is not a valid email address"); } else{ echo("This is a valid email address"); } ?> |
Using: FILTER_VALIDATE_EMAIL filter validates an e-mail address:
1 2 3 4 5 6 7 8 9 10 |
<?php $email = "admin@techarise.com"; // Validate e-mail if (filter_var($email, FILTER_VALIDATE_EMAIL)) { echo("$email is a valid email address"); } else{ echo("$email is not a valid email address"); } ?> |