簡體   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