duanji9677 2010-08-14 21:04
浏览 36
已采纳

PHP服务器端验证正则表达式匹配

I have the following part of a validation script:

$invalidEmailError .= "<br/>&raquo;&nbsp;You did not enter a valid E-mail address";    
$match = "/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/";

That's the expression, here is the validation:

if ( !(preg_match($match,$email)) ) {
    $errors .= $invalidEmailError; // checks validity of email
}

I think that's enough info, let me know if more is needed.

Basically, what happens is the message "You did not enter a valid E-mail address" gets echoed no matter what. Whether a correct email address or an incorrect email address is entered.

Does anyone have any idea or a clue as to why?

EDIT: I'm running this on localhost (using Apache), could that be the reason as to why the preg_match ain't working?

Thanks! Amit

  • 写回答

3条回答 默认 最新

  • doutan1857 2010-08-14 21:08
    关注

    Your regex only includes [A-Z], not [a-z]. Try

    $match = "/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i";
    

    to make the regex case-insensitive.

    You can test this live on http://regexpal.com.

    However, I'd advise you to try one of the expressions on the page mentioned by strager: http://fightingforalostcause.net/misc/2006/compare-email-regex.php. They have been perfected over time and will probably behave better. But Gmail users will be satisfied with yours, since they'll be able to use plus aliases which are rejected incorrectly by many validators.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统
  • ¥15 快手联盟怎么快速的跑出建立模型