开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜