toggle menu

Why FILTER_VALIDATE_EMAIL not working in PHP - Complete Solution Here

Ravinder |26 Jun at 01:06

FILTER_VALIDATE_EMAIL  function not working

See below mention example:

$email = "abc@domain.c";

if (filter_var($email, FILTER_VALIDATE_EMAIL))

        echo "Email: ".$email." correct";

else

        echo "email not correct"; 

 

//It returns:

Email: abc@domain.c correct //which is invalid email id

?>

 

Why FILTER_VALIDATE_EMAIL not work?

Because FILTER_VALIDATE_EMAIL not accepting Internationalized Domain Names and not allowed all characters according to RFC2822, so it might be better to use regex in php to validate email address instead of filter_validate_email.

 

Email validation in regex in php:

function valid_email($str) {

return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;

}

 

if(!valid_email("abc@domain.c")){

echo "Invalid email address.";

}else{

echo "Valid email address.";

}?>

 

  • 1 like
  • 0 comment

Posted:

26 Jun at 01:06

Viewed:

3466 times

Active:

26 Jun at 01:06