having trouble validating email addresses in php
-
19-09-2019 - |
Question
I can't figure out where I'm going wrong here. Trying to validate an email address:
if (!preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $emailREG)) {
Solution
Try using FILTER_VALIDATE_EMAIL instead:
<?php
$email = "someone@exa mple.com";
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
echo "E-mail is not valid";
}else{
echo "E-mail is valid";
}
?>
OTHER TIPS
Be careful with filter_var with FILTER_VALIDATE_EMAIL. "somebody@example" will be validated as a valid email address because, well, it is a valid email address but may not be desirable.
You can also use the validate PEAR package.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow