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

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

我用这个正则表达式,

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

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


当前回答

其中str可以是以下格式中的任何一种: 555-555-5555 (555) 555 - 5555 (555) 555 - 5555 555 555 5555 5555555555 1 555 555 5555

函数电话检查(str) { var isphone = /^(1\s|1|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3})(\-|\s)?(\d{4})$/.test(str); 警报(电话); } 电话检查(“1 555 555 5555”);

其他回答

试试这个

/^(\+{0,})(\d{0,})([(]{1}\d{1,3}[)]{0,}){0,}(\s?\d+|\+\d{2,3}\s{1}\d+|\d+){1}[\s|-]?\d+([\s|-]?\d+){1,2}(\s){0,}$/gm

有效的格式

(123) 456-7890 (123)456-7890 The 123-456-7890 1234567890 + 31636363634 +3(123) 123-12-12 + 3 (123) 123-12-12 + 3 (123)1231212 +3(123) 12312123 Plus 3(123), 123, 12, 12 075-63546725 +7910 120 54 54 910 120 54 54 8 999 999 99 99

https://regex101.com/r/QXAhGV/1

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

$("#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;
}

这是可行的:

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

的吗?字符表示前一组匹配0次或1次。组(-|\s)将匹配-或|字符。添加?在正则表达式中第二次出现此组后,允许您匹配10个连续数字的序列。

\\(?\d{3}\\)?([\-\s\.])?\d{3}\1?\d{4}

这将验证任何可变格式的电话号码:

\ \ (? \ d {3} \ \) ?找到3个被括号括起来的数字。

(\ [\ s \]) ?是否找到这些分隔符

\d{3}找到3个数字

\1使用第一个匹配的分隔符-这确保分隔符是相同的。因此(000)999-5555在这里将不生效,因为有空格和破折号分隔符,所以只需删除“\1”并替换为分隔符子模式(这样做也将验证非标准格式)。无论如何,您都应该为用户输入提供格式提示。

\d{4}找到4个数字

验证:

(000) 999 5555 (000) 999-5555 (000). 999.5555 (000) 999-5555 (000)9995555 000 999 5555 The 000-999-5555 000.999.5555 0009995555

顺便说一句,这是JavaScript的双重转义。

阿根廷数据的RegExp: / ^((+[0 - 9]{1,3})) ?(([0 - 9]{8、13})|([0 - 9]{3、6})——([0 - 9]{4 7}))/美元

Valids:

X: from 8 to 13 numbers

XXXXXXXX

separate numbers by a hyphen = X from 3 to 6; Y: from 4 to 7

XXXX-YYYY

Z: from 1 to 3

(+ Z) XXXXXXXX

if you use Z then (+ ) is mandatory.