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

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

我用这个正则表达式,

/^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{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);
}

其他回答

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

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

这是一个非常松散的选项,我更喜欢保持这种方式,大多数情况下,我在注册表单中使用它,用户需要添加他们的电话号码。 通常用户对强制执行严格格式规则的表单有麻烦,我更喜欢用户在显示或保存到数据库之前填写数字和格式。 http://regexr.com/3c53v

/ s ^ + 1 ? \ * d {3 \ (? ? }(?:\)|[-|\ s]) \ s * ? [d {3} - | \ s] ?\ d{4} -美元

这篇文章虽然老了,但还是想留下我的一点贡献。 这些是可以接受的: 5555555555 555-555-5555 (555) 555 - 5555 1 (555) 555 - 5555 1 555 555 5555 1 555-555-5555 1 (555) 555-5555

这些是不被接受的:

555 - 5555 >接受这个用:^ \ + ? 1 ? \ s * ? \ (? (\ d {3 })?(?:\)|[-|\ s]) ? \ s * ? \ d {3} (- | \ s) ?\ d {4} $

5555555 - - - >接受这个用:^ \ + ? 1 ? \ s * ? \ (? (\ d {3 })?(?:\)|[-|\ s]) ? \ s * ? \ d {3} (- | \ s) ?\ d {4} $

1 555) 555 - 5555 123 * * & ! !asdf # 55555555 (6505552368) 2 (757) 622-7382 0 (757) 622-7382 -1 (757) 622-7382 2 757 622-7382 10 (757) 622-7382 27576227382 (275) 76227382 2 (757) 6227382 2 (757) 622 - 7382 (555) 5 (55) -5555

这是我使用的代码:

function telephoneCheck(str) {
  var patt = new RegExp(/^\+?1?\s*?\(?\d{3}(?:\)|[-|\s])?\s*?\d{3}[-|\s]?\d{4}$/);
  return patt.test(str);
}

telephoneCheck("+1 555-555-5555");

我不得不承认,核实电话号码是一项艰巨的任务。 至于这个具体的问题,我会改变正则表达式从

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

to

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

因为唯一多余的元素是最后的破折号/空格。

试试这个js函数。 如果匹配则返回true,如果失败则返回false 裁判

函数ValidatePhoneNumber(电话){ 返回/ ^ \ + ?([0 - 9]{2})\)?(-。] ?([0 - 9]{4})[-。] ?([0 - 9]{4})美元/ test(电话); }

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

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