简体   繁体   English

用于验证电话号码的 Javascript 正则表达式

[英]Javascript regex to validate a phone number

I am working to modify a script.我正在修改脚本。 The script contains a form to allow visitors to send an SMS to phone numbers.该脚本包含一个表单,允许访问者向电话号码发送 SMS。 In the form I have a text box in which users enter the phone number of the text receiver.在表单中,我有一个文本框,用户可以在其中输入文本接收者的电话号码。 I am using a regex to validate the phone number to prevent spammers and make sure the user typed the correct number.我正在使用正则表达式来验证电话号码以防止垃圾邮件发送者并确保用户输入正确的号码。

Here are the default phone numbers used in Afghanistan:以下是阿富汗使用的默认电话号码:

+93785657024
+93795657024
+93700565656
+93775657024

The regex validation first should make sure that +93 is used, then make sure that 78, 77, 79 or 700 (one of these) is used after +93 and finally followed by 6 digits.正则表达式验证首先应确保使用 +93,然后确保在 +93 之后使用 78、77、79 或 700(其中之一),最后是 6 位数字。

Here is the Javascript code I am trying to fix.这是我正在尝试修复的 Javascript 代码。 This code now works only for +937 followed by 8 digits.此代码现在仅适用于 +937 后跟 8 位数字。

<script type="text/javascript">
function test (){
  var phoneRegex = new RegExp('^937\d{8}$');
  var phoneNum = document.getElementById("receiver").value;
  if(!(phoneRegex.test(phoneNum))) {
    alert("Invalid Phone Number");
    document.getElementById("receiver").focus();
  } else {
    alert("valid");
  }
}
</script>

The regex validation first should make sure that +93 is used, then make sure that 78, 77, 79 or 700 (one of these) is used after +93 and finally followed by 6 digits.正则表达式验证首先应确保使用 +93,然后确保在 +93 之后使用 78、77、79 或 700(其中之一),最后是 6 位数字。


This answer will not work except in the 700 case because the OP specified that all prefixes were followed by 6 digits, when in fact only 93700 is followed by 6, and the other prefixes are followed by 7 digits.除了 700 的情况外,此答案不起作用,因为 OP 指定所有前缀后跟 6 位数字,而实际上只有 93700 后跟 6 位,其他前缀后跟 7 位数字。

/^\+93(77\d|78\d|79\d|700)\d{6}$/

should do it.应该这样做。

Your original regex doesn't require a "+" at the front though.不过,您的原始正则表达式不需要前面的“+”。

I think this would suit your condition我认为这适合你的情况

Totally you need 11 digits prefixed with +.总共需要 11 位以 + 为前缀的数字。

Therefore if 77|78|79 means followed by 7 digits or if 700, followed by 6 digits.因此,如果 77|78|79 表示后跟 7 位数字,或者如果 700 表示后跟 6 位数字。

var reg = /^\+93((77|78|79)\d{7}|700\d{6})$/;
console.log(reg.test('+93705657024'));

Check this Fiddle检查这个小提琴

Regular expression itself:正则表达式本身:

var phoneRegex = new RegExp('(937(?:00\\d{6}|[789]\\d{7}))');

Notes:笔记:

  • Since you are using new RegExp , I have not inserted / and / around the expression.由于您使用的是new RegExp ,我没有在表达式周围插入// (I am mentioning this because you commented that some of the other answers did not work at all, and I suspect that is because you may have forgotten to remove the / and / before testing them, perhaps? There seem to be minor problems with them which is why I am also answering, but the other answers should at least match some valid phone numbers if you remove the / and / that surround the regular expressions.) (我提到这一点是因为您评论说其他一些答案根本不起作用,我怀疑这是因为您可能在测试之前忘记删除 / 和 / ,也许?它们似乎存在小问题这就是为什么我也在回答,但如果您删除正则表达式周围的 / 和 /,其他答案至少应该匹配一些有效的电话号码。)
  • You seem to have made a mistake in counting the digits vs. the examples you gave of valid Afghanistan phone numbers.您在计算数字与您提供的有效阿富汗电话号码的示例时似乎犯了错误。 +9377, +9378, etc. are followed by 7 digits rather than 6, while only +93700 is actually followed by 6. +9377、+9378等后面是7位数字而不是6位,而实际上只有+93700后面是6位。
  • My expression puts the full phone number into the temporary variable $1, which can come in handy.我的表达式将完整的电话号码放入临时变量 $1 中,这可以派上用场。 (I used a non-capturing group for the alternatives in the second part of the match because extra captures sometimes can be a bother when I want to do something else with the results of the match. For what your code is doing, it would work the same with or without the ?: that my regular expression contains.) (我在比赛的第二部分使用了一个非捕获组作为替代方案,因为当我想对比赛结果做其他事情时,额外的捕获有时会很麻烦。对于您的代码正在做什么,它会起作用有或没有?: ,我的正则表达式包含。)
  • My expression does not anchor the match to the beginning or end of the string, because doing so would return false for valid numbers that had punctuation, like the + that you showed in your own phone numbers.我的表达式不会将匹配锚定到字符串的开头或结尾,因为这样做会为带有标点符号的有效数字返回false ,例如您在自己的电话号码中显示的+
  • For the same reason, I suggest that you delete non-numeric characters before checking input for a valid phone number.出于同样的原因,我建议您在检查输入的有效电话号码之前删除非数字字符。 Right now phone numbers using parentheses or hyphens or any traditional punctuation will return false even though they are actually valid.现在,使用括号或连字符或任何传统标点符号的电话号码将返回false即使它们实际上是有效的。
  • This modification of your code would work for ensuring that valid phone numbers are recognized correctly by first removing punctuation:通过首先删除标点符号,对代码的这种修改将有助于确保正确识别有效的电话号码:

     if(!(phoneRegex.test(phoneNum.replace(/\\D+/g, '')))) {
  • If you make this modification to your code, then it would be appropriate to add the anchoring expressions ^ and $ around my suggested regular expression to make sure it only contained a valid phone number, not a longer number such as a credit card number.如果您对代码进行了这种修改,那么在我建议的正则表达式周围添加锚定表达式^$是合适的,以确保它只包含有效的电话号码,而不是更长的号码,例如信用卡号码。

There is a problem with the way your code checks the results from doing the regular expression test.您的代码检查正则表达式测试结果的方式存在问题。 This code will work better for you:此代码将更适合您:

<script type="text/javascript">
function test (){
  var phoneField = document.getElementById("receiver")
  var phoneNum = phoneField.value;
  if (/^(937(?:00\d{6}|[789]\d{7}))$/.test(phoneNum.replace(/\D+/g, ''))) {
  alert('valid');
  } else {
  alert('Invalid phone number');
  phoneField.focus();
  }
}
</script>
<input id="receiver" value="+93785657024" />
<input type="button" onclick="test()" value="Check this number" />

Note: I got some of my code mixed up.注意:我弄混了一些代码。 Please ignore my other code (the one that was uploaded to that link, I mean).请忽略我的其他代码(我的意思是上传到该链接的代码)。 This one works.这个有效。

Use:采用:

/^\+937([789]|00)\d\d\d\d\d\d$/

That should do it.那应该这样做。

+937 All your numbers start with this +937 你所有的号码都以此开头
([789]|00) Either 7, 8, 9 or 00 ([789]|00) 7、8、9 或 00
\\d\\d\\d\\d\\d 6 digits \\d\\d\\d\\d\\d 6 位数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM