简体   繁体   English

使用PHP进行正则表达式电话号码验证

[英]regex phone number validation with PHP

This is another question about a previous question I had asked yesterday . 这是我昨天问的先前问题的另一个问题。 I want user to be allowed to type US phone numbers in the following formats. 我希望允许用户以以下格式键入美国电话号码。

(800)-555-1212 (800)-555-1212

800-555-1212 800-555-1212

and have it only check the database for numbers 8005551212 并只检查数据库中的号码8005551212

I've seen that regex like /^[\\+]?([0-9]*)\\s*\\(?\\s*([0-9]{3})?\\s*\\)?[\\s\\-\\.]*([0-9]{3})[\\s\\-\\.]*([0-9]{4})[a-zA-Z\\s\\,\\.]*[x\\#]*[a-zA-Z\\.\\s]*([\\d]*)/ 我已经看到正则表达式如/^[\\+]?([0-9]*)\\s*\\(?\\s*([0-9]{3})?\\s*\\)?[\\s\\-\\.]*([0-9]{3})[\\s\\-\\.]*([0-9]{4})[a-zA-Z\\s\\,\\.]*[x\\#]*[a-zA-Z\\.\\s]*([\\d]*)/

may work but I'm not certain how to implement it into the code from the link I provided 可能有效,但我不确定如何从我提供的链接中将其实现到代码中

I'm new to php and know nothing about regex. 我是php新手,对regex一无所知。 Any help is appreciated. 任何帮助表示赞赏。

This function validate a phone number, return true if it validate and false if invalid. 此功能验证电话号码,如果验证有效,则返回true;如果无效,则返回false。 This function very simple i was wrote to. 这个功能非常简单,我被写到了。

    /**
     * @param $number
     *
     * @return bool
     */
    function validatePhoneNumber($number) {
        $formats = [
            '###-###-####', '####-###-###',
            '(###) ###-###', '####-####-####',
            '##-###-####-####', '####-####', '###-###-###',
            '#####-###-###', '##########', '#########',
            '# ### #####', '#-### #####'
        ];

        return in_array(
            trim(preg_replace('/[0-9]/', '#', $number)),
            $formats
        );
    }

if javascript is ok can go with 如果javascript没问题,可以使用

<script type="text/javascript">
function matchClick() {
  var re = new RegExp("Your regex here");
  if (document.formname.phone.value.match(re)) {
     alert("Ok");
     return true;
  } else {
     alert("Not ok");
     return false;
  }
} 
</script>

call this function onsubmit of form or onblur of textbox 在表单的提交或文本框的模糊处理中调用此函数

If you have doubt about your regex you can validate it at http://www.regular-expressions.info/javascriptexample.html 如果您对自己的正则表达式有疑问,可以在http://www.regular-expressions.info/javascriptexample.html上进行验证

您可以使用此模式

\\(?\\d{3,3}\\)?-\\d{3,3}-\\d{4,4}

Try this, 尝试这个,

<?php
    $t='/\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}/';
    $arr=preg_match($t,'(800)-555-1212',$mat);
    $arr=preg_match($t,'800-555-1212',$mat);
    print_r($mat);
?>

Tested here 在这里测试

Re: Rohan Kumar's solution 回复:Rohan Kumar的解决方案

<?php
$t='/\(?[2-9][0-8][0-9]\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}/';
$arr=preg_match($t,'(800)-555-1212',$mat);
$arr=preg_match($t,'800-555-1212',$mat);
print_r($mat);
?>

It does address the issue of fake phone numbers such as 800-123-2222. 确实解决了假电话号码(例如800-123-2222)的问题。 Real phone numbers have a first digit of at least "2". 真实电话号码的第一位数字至少为“ 2”。 While the other solutions do the format correctly, they don't address the issue of people putting in phone numbers like 800-000-1234, which would be correct in the other solutions provided. 尽管其他解决方案正确地设置了格式,但它们不能解决人们输入电话号码(例如800-000-1234)的问题,这在所提供的其他解决方案中是正确的。

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

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