[英]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'));
Regular expression itself:正则表达式本身:
var phoneRegex = new RegExp('(937(?:00\\d{6}|[789]\\d{7}))');
Notes:笔记:
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.) ?:
that my regular expression contains.) ?:
,我的正则表达式包含。)false
for valid numbers that had punctuation, like the +
that you showed in your own phone numbers.false
,例如您在自己的电话号码中显示的+
。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.