validate email using RE + php code example
Example 1: php preg_match email validation code
<?php
function checkemail($str) {
return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
if(!checkemail("[email protected]")){
echo "Invalid email address.";
}
else{
echo "Valid email address.";
}
?>
Example 2: how to validate an email field using php
<?php
$email = "[email protected]";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo("$email is a valid email address");
}
else{
echo("$email is not a valid email address");
}
?>