我在一些网站上找到了这个代码,它工作得很完美。它验证电话号码是以下格式之一: (123) 456-7890或123-456-7890

问题是我的客户端(我不知道为什么,可能是客户端)想要添加另一种格式,连续的十个数字,像这样:1234567890。

我用这个正则表达式,

/^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/

我如何添加它也验证另一种格式?我不擅长使用正则表达式。


当前回答

首先,格式验证器显然只适用于NANP(国家代码+1)数字。您的应用程序是否会被拥有北美以外电话号码的人使用?如果是这样,你不想阻止这些人输入完全有效的(国际)号码。

其次,你的验证是不正确的。NANP数字的形式为NXX NXX XXXX,其中N为数字2-9,X为数字0-9。此外,区号和交换机不能采用N11形式(以两个1结尾),以避免与特殊服务混淆,除非非地理区号(800,888,877,866,855,900)中的号码可以有N11交换机。

因此,您的正则表达式将传递数字(123)123 4566,即使这不是一个有效的电话号码。你可以通过用[2-9]{1}\d{2}替换\d{3}来解决这个问题。

最后,我感觉你是在web浏览器中验证用户输入。请记住,客户端验证只是您为用户提供的便利;您仍然需要(再次)验证服务器上的所有输入。

TL;DR不使用正则表达式来验证复杂的现实数据,如电话号码或url。使用专门的库。

其他回答

下面的REGEX将验证这些格式中的任何一个:

(123) 456-7890 The 123-456-7890 123.456.7890 1234567890

/^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/

只是想添加一个解决方案,专门选择非本地电话号码(800和900类型)。

(\+?1[-.(\s]?|\()?(900|8(0|4|5|6|7|8)\3+)[)\s]?[-.\s]?\d{3}[-.\s]?\d{4}

如果你使用输入标签,这段代码将帮助你。这段代码是我自己写的,我认为这是很好的输入方式。但是你可以用你的格式来改变它。它将帮助用户纠正输入标签上的格式。

$("#phone").on('input', function() {  //this is use for every time input change.
        var inputValue = getInputValue(); //get value from input and make it usefull number
        var length = inputValue.length; //get lenth of input

        if (inputValue < 1000)
        {
            inputValue = '1('+inputValue;
        }else if (inputValue < 1000000) 
        {
            inputValue = '1('+ inputValue.substring(0, 3) + ')' + inputValue.substring(3, length);
        }else if (inputValue < 10000000000) 
        {
            inputValue = '1('+ inputValue.substring(0, 3) + ')' + inputValue.substring(3, 6) + '-' + inputValue.substring(6, length);
        }else
        {
            inputValue = '1('+ inputValue.substring(0, 3) + ')' + inputValue.substring(3, 6) + '-' + inputValue.substring(6, 10);
        }       
        $("#phone").val(inputValue); //correct value entered to your input.
        inputValue = getInputValue();//get value again, becuase it changed, this one using for changing color of input border
       if ((inputValue > 2000000000) && (inputValue < 9999999999))
      {
          $("#phone").css("border","black solid 1px");//if it is valid phone number than border will be black.
      }else
      {
          $("#phone").css("border","red solid 1px");//if it is invalid phone number than border will be red.
      }
  });

    function getInputValue() {
         var inputValue = $("#phone").val().replace(/\D/g,'');  //remove all non numeric character
        if (inputValue.charAt(0) == 1) // if first character is 1 than remove it.
        {
            var inputValue = inputValue.substring(1, inputValue.length);
        }
        return inputValue;
}

试试这个——它也包括国际格式的验证。

/^[+]?(1\-|1\s|1|\d{3}\-|\d{3}\s|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3})(\-|\s)?(\d{4})$/g

这个正则表达式验证以下格式:

(541) 754-3010国内 + 1-541-754-3010国际 拨打美国电话1-541-754-3010 从德国拨001-541-754-3010 191 541 754 3010从法国拨过来

首先,格式验证器显然只适用于NANP(国家代码+1)数字。您的应用程序是否会被拥有北美以外电话号码的人使用?如果是这样,你不想阻止这些人输入完全有效的(国际)号码。

其次,你的验证是不正确的。NANP数字的形式为NXX NXX XXXX,其中N为数字2-9,X为数字0-9。此外,区号和交换机不能采用N11形式(以两个1结尾),以避免与特殊服务混淆,除非非地理区号(800,888,877,866,855,900)中的号码可以有N11交换机。

因此,您的正则表达式将传递数字(123)123 4566,即使这不是一个有效的电话号码。你可以通过用[2-9]{1}\d{2}替换\d{3}来解决这个问题。

最后,我感觉你是在web浏览器中验证用户输入。请记住,客户端验证只是您为用户提供的便利;您仍然需要(再次)验证服务器上的所有输入。

TL;DR不使用正则表达式来验证复杂的现实数据,如电话号码或url。使用专门的库。