having trouble validating email addresses in php
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)) {
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";
}
?>
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.
精彩评论