繁体   English   中英

PHP正则表达式无法正常工作

[英]PHP regular expression not working properly

因此,我尝试以xxx-xxx-xxxx的形式验证电话号码,其中x是数字。 我遇到的问题是我可以为最后一组数字输入4位以上的数字。 有人看到我在做什么错吗? 谢谢。

    $telNumPattern = '/[0-9]{3}-[0-9]{3}-[0-9]{4}/'; //regular expression to match xxx-xxx-xxxx
    if(empty($_POST['telNum'])) {
        $errors['telNum'] = "*Your telephone number can't be empty";
    }
    else if(!preg_match($telNumPattern, $_POST['telNum'])) {
        $errors['telNum'] = "*Your email must be in the form xxx-xxx-xxxx";
    }
$telNumPattern = '/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/';

使用锚点进行精确匹配。

试试这个它的工作:

$telNumPattern = '/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/'; //regular expression to match xxx-xxx-xxxx
    if(empty($_POST['telNum'])) {
        $errors['telNum'] = "*Your telephone number can't be empty";
    }
    else if(!preg_match($telNumPattern, $_POST['telNum'])) {
        $errors['telNum'] = "*Your email must be in the form xxx-xxx-xxxx";
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM