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

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

我用这个正则表达式,

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

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


当前回答

我使用了@marty提供的一个,并对它进行了一些修改,以适应更多的情况:

/^[\+]?([0-9][\s]?|[0-9]?)([(][0-9]{3}[)][\s]?|[0-9]{3}[-\s\.]?)[0-9]{3}[-\s\.]?[0-9]{4,6}$/im

有效的格式:

+1 123 4567890 + 11234567890 + 1 (123)4567890 + 1 (123)456-7890 +1 (123) 456-7890 +1 (123)456 7890 +1 123 456-7890 +1 123 456 7890 +1 123-456-7890 The 123-456-7890 123 456-7890 123 456 7890 123 4567890 1234567890

其他回答

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

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

我选择的正则表达式是:

/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im

有效的格式:

(123) 456-7890 (123)456-7890 The 123-456-7890 123.456.7890 1234567890 + 31636363634 075-63546725

每个人的答案都很棒,但我认为这里有一个更全面的答案……

这是为javascript匹配使用一个单一的数字在单行:

^(?!.*911.*\d{4})((\+?1[\/ ]?)?(?![\(\. -]?555.*)\( ?[2-9][0-9]{2} ?\) ?|(\+?1[\.\/ -])?[2-9][0-9]{2}[\.\/ -]?)(?!555.?01..)([2-9][0-9]{2})[\.\/ -]?([0-9]{4})$

如果您想在单词边界上匹配,只需将^和$更改为\b

我欢迎对这个解决方案提出任何建议、纠正或批评。据我所知,这与NANP格式(对于美国数字-我在创建此时没有验证其他北美国家)匹配,避免任何911错误(不能在区号或地区代码中),仅消除那些实际上无效的555数字(555的地区代码后面是01xx,其中x =任何数字)。

下面的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}$/

我要做的是忽略格式并验证数字内容:

var originalPhoneNumber = "415-555-1212";

function isValid(p) {
  var phoneRe = /^[2-9]\d{2}[2-9]\d{2}\d{4}$/;
  var digits = p.replace(/\D/g, "");
  return phoneRe.test(digits);
}