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条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了