dongyaofu0599 2012-07-26 15:18
浏览 96
已采纳

用于验证电子邮件地址的正则表达式帮助

I am trying to validate an email field. I took this regex from somewhere on here for and I used it on another form I made and it works fine. Yet when I use it now its not matching.

All I am trying to do is to check the email and if it is good then log it in the proper field in the db.

For the sake of not pasting a bunch of stuff... I have stripped out the problem lines and going to pseudo code next few lines.

Essentially, vars are these:

$theEmail = $_post email from first page here
$regEx ='#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si';

and my php is this

//essentially other field validation will go here...for now testing only empty.
    if(!empty($theEmail)){
        if (preg_match($regEx, $formEmail)) {
            //send it through to db.

        } else { //error stuff here }
    }

essentially, this never comes true. The email never validates no matter what I do and as I said I wrote another more complicated form that validates data just fine

Not sure what is going on.

  • 写回答

4条回答 默认 最新

  • doubeiji2602 2012-07-26 15:25
    关注

    /^[a-z0-9.!\#$%&\'*+-=?^_{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$/

    I removed the first # and ending #si, and took out the / from the = since it was giving me problems. This generates a match on my e-mail address here:

    <?
    $theEmail = 'me@davebel.com';
    $regEx ='/^[a-z0-9.!\#$%&\'*+-=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$/';
    print_r(preg_match($regEx, $theEmail));
    ?>
    

    Though this regex is very complex for something like e-mail validation- I would recommend trying to refine it and fine-tune it before putting it into production.

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法