Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 电话号码验证_Php_Validation_Preg Match - Fatal编程技术网

Php 电话号码验证

Php 电话号码验证,php,validation,preg-match,Php,Validation,Preg Match,可能重复: 我试图使用preg_match验证电话号码,但每当我键入正确格式的电话号码(111)111-1111时,它都会返回无效字符错误,而不是true。我认为我的正则表达式没有任何问题(据我所知),所以我猜我的逻辑有问题 function validate_phone_number($phoneNumber, $requiredLength = 14) { //Check to make sure the phone number format is valid

可能重复:

我试图使用preg_match验证电话号码,但每当我键入正确格式的电话号码(111)111-1111时,它都会返回无效字符错误,而不是true。我认为我的正则表达式没有任何问题(据我所知),所以我猜我的逻辑有问题

function validate_phone_number($phoneNumber, $requiredLength = 14)
{

       //Check to make sure the phone number format is valid 
    for ($i = 0; $i < strlen($_POST[$phoneNumber]); $i++){
            if(preg_match('/^\(\d{3}\) \d{3}-\d{4}\$/', $_POST[phoneNumber]{$i}))
            {
                return true;
            }
            else
            {
                return "<h3>" . "The phone number you entered contains invalid characters" . "</h3>";
            }

       //Check to make sure the number is the required length
            if (strlen($_POST[$phoneNumber]) > $requiredLength) {
                return "<h3>" . "The phone number you entered contains too many characters" . "</h3>";
            }
            else if (strlen($_POST[$phoneNumber]) < $requiredLength) {
                return "<h3>" . "The phone number you entered does not contain enough characters" . "</h3>";
            }
        }
        return true;
}
函数验证电话号码($phoneNumber,$requiredLength=14)
{
//检查以确保电话号码格式有效
对于($i=0;$i$requiredLength){
return“。”您输入的电话号码包含的字符太多“。”;
}
else if(strlen($\u POST[$phoneNumber])<$requiredLength){
return“.”您输入的电话号码没有包含足够的字符。“”;
}
}
返回true;
}
我用什么来调用函数

if (count($_POST) > 0) {
    $error = array();


   $phone = validate_phone_number('phoneNumber');
        if($phone !==true) {
            $error[] = $phone;

        }


        if (count($error) == 0) {

           //Phone number validates

        }

        else { 
          echo "<h2>Error Message:</h2>";

          foreach($error as $msg) {
            echo "<p>" . $msg . "</p>";
          }
        }
      }
if(计数($\u POST)>0){
$error=array();
$phone=验证电话号码(“电话号码”);
如果($phone!==true){
$error[]=$phone;
}
如果(计数($error)==0){
//电话号码验证
}
否则{
回显“错误消息:”;
foreach($msg错误){
回声“”$msg.

”; } } }
这里有两件事不对:

for ($i = 0; $i < strlen($_POST[$phoneNumber]); $i++){
        if(preg_match('/^\(\d{3}\) \d{3}-\d{4}\$/', $_POST[phoneNumber]{$i}))

长度检查完全是多余的,因为正则表达式已经声明了您的固定格式。

我看到的两个主要方面:

正则表达式有问题:

'/^\(\d{3}\) \d{3}-\d{4}\$/'
                        ^- wrong. leave it out:

'/^\(\d{3}\) \d{3}-\d{4}$/'
当您首先测试正则表达式时,它将仅在字符串正好为14个字符时匹配。因此,之后不需要检查字符串长度

你也应该考虑使用这个函数,例如:

$options['options'] = array('regexp' => '/^\(\d{3}\) \d{3}-\d{4}$/');
$valid = filter_var($number, FILTER_VALIDATE_REGEXP, $options);
它还有一个姐妹函数,可以对
$\u POST
输入进行操作。可能很方便:

$options['options'] = array('regexp' => '/^\(\d{3}\) \d{3}-\d{4}$/');
$valid = filter_input(
    INPUT_POST, 'phoneNumber', FILTER_VALIDATE_REGEXP, $options
);

很多电话号码都不适合这种格式。我发现了一件似乎是错误的事情:
validate\u phone\u number()
依赖于
$\u POST
。将函数需要的所有内容作为参数传递给word。这可能是错误,您可能已经想用第一个函数参数替换它。我假设您是一个仅限美国的网站?我之所以提到这一点,是因为你使用的电话格式对世界上大多数国家来说都是错误的。在接受马里奥和哈克雷的建议之间,它起了作用!谢谢各位。如果我的声望够高的话,我会投票的。我知道只使用这种格式不是一件好事,但这只是为了练习。布里安-我代表你给他们投了赞成票-你也投了赞成票。欢迎加入董事会。这个正则表达式在英国不起作用。@Dagon-Bugger-我错过了那个会议!星期一很早吗?顺便说一句——一个英国人上网了——我是选择住在苏格兰的英国人。
$options['options'] = array('regexp' => '/^\(\d{3}\) \d{3}-\d{4}$/');
$valid = filter_input(
    INPUT_POST, 'phoneNumber', FILTER_VALIDATE_REGEXP, $options
);