Javascript 仅限正则表达式编号,破折号是可选的

Javascript 仅限正则表达式编号,破折号是可选的,javascript,regex,Javascript,Regex,我正在尝试使用正则表达式创建一个javascript函数,它将验证一个电话号码 规则是: 1.只有数字。 2.超过10个数字。 3.允许使用破折号(-)(可选) 首先,我试过这个: function validatePhone(phone) { var phoneReg = /[0-9]{10,}/; return (phoneReg.test(phone)); } 它只在前两条规则上效果很好,但在短跑上效果不佳 然后我尝试了var phoneRe

我正在尝试使用正则表达式创建一个javascript函数,它将验证一个电话号码

规则是:
1.只有数字。 2.超过10个数字。 3.允许使用破折号(-)(可选)

首先,我试过这个:

  function validatePhone(phone) {

        var phoneReg = /[0-9]{10,}/;
        return (phoneReg.test(phone));
    }
它只在前两条规则上效果很好,但在短跑上效果不佳

然后我尝试了
var phoneReg=/[-0-9]{10,}/
甚至
var phoneReg=[\d]+\-?[\d]+
但是javascript被破坏了


有什么想法吗?

这是我如何验证电话号码的方法:

var validatePhone = function(phone) {

  // Stip everything but the digits.
  // People like to format phone numbers in all
  // sorts of ways so we shouldn't complain
  // about any of the formatting, just ensure the
  // right number of digits exist.
  phone = phone.replace(/\D/g, '');

  // They should have entered 10-14 digits.
  // 10 digits would be sans-country code,
  // 14 would be the longest possible country code of 4 digits.
  // Return `false` if the digit range isn't met.
  if (!phone.match(/\d{10,14}/)) return false;

  // If they entered 10, they have left out the country code.
  // For this example we'll assume the US code of '1'.
  if (phone.length === 10) phone = '1' + phone;

  // This is a valid number, return the stripped number
  // for reformatting and/or database storage.
  return phone;
}

以下是我如何进行电话号码验证的方法:

var validatePhone = function(phone) {

  // Stip everything but the digits.
  // People like to format phone numbers in all
  // sorts of ways so we shouldn't complain
  // about any of the formatting, just ensure the
  // right number of digits exist.
  phone = phone.replace(/\D/g, '');

  // They should have entered 10-14 digits.
  // 10 digits would be sans-country code,
  // 14 would be the longest possible country code of 4 digits.
  // Return `false` if the digit range isn't met.
  if (!phone.match(/\d{10,14}/)) return false;

  // If they entered 10, they have left out the country code.
  // For this example we'll assume the US code of '1'.
  if (phone.length === 10) phone = '1' + phone;

  // This is a valid number, return the stripped number
  // for reformatting and/or database storage.
  return phone;
}

这应该行得通。
-
字符需要转义

var phoneReg = /[0-9-\-]{11,}/;
这样做的潜在问题是,即使字符串中没有10个数字,具有多个破折号的字符串也将测试为正。我建议在测试前更换破折号

var phoneReg = /[0-9]{11,}/;
return (phoneReg.test(phone.replace(/\-/g, '')); 

这应该行得通。
-
字符需要转义

var phoneReg = /[0-9-\-]{11,}/;
这样做的潜在问题是,即使字符串中没有10个数字,具有多个破折号的字符串也将测试为正。我建议在测试前更换破折号

var phoneReg = /[0-9]{11,}/;
return (phoneReg.test(phone.replace(/\-/g, '')); 

“我正在尝试使用正则表达式生成一个javascript函数,该函数将验证电话号码。”这是您的第一个错误。:-)即使在美国,电话号码也可能很复杂(分机等等),如果你正在处理任何跨越国家界线的事情,那么所有的赌注都没有了。通常最好让人们写他们写的东西,确保它不是空白的,并让他们仔细检查。FWIW.@T.J.Crowder谢谢你,但这只是我的国家。这是非常特别的。再次感谢您的关心。@thormayer:您的国家没有分机吗?例如,
工作号码:[01234567891 ext 332]
?“我试图用正则表达式生成一个javascript函数,用于验证电话号码。”这是您的第一个错误。:-)即使在美国,电话号码也可能很复杂(分机等等),如果你正在处理任何跨越国家界线的事情,那么所有的赌注都没有了。通常最好让人们写他们写的东西,确保它不是空白的,并让他们仔细检查。FWIW.@T.J.Crowder谢谢你,但这只是我的国家。这是非常特别的。再次感谢您的关心。@thormayer:您的国家没有分机吗?例如,
工作编号:[01234567891分机332]
2。超过10个数字
那么它不应该是
[0-9]{11,}
2。超过10个数字
那么它不应该是
[0-9]{11,}