在 JavaScript 中验证 11 位数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9186275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate 11 Digits in JavaScript
提问by Rabiani
Might be a simple question, but I can't find an answer that fits this bit of code :(
可能是一个简单的问题,但我找不到适合这段代码的答案:(
I need to validate a phone number to have EXACTLY 11 digits (no letters)
我需要验证电话号码是否有 11 位数字(无字母)
function validateForm3() {
var x=document.forms["booking"]["number"].value;
if (x==null || x=="")
{
alert("Please fill in 11 numbers");
return false;
}
var x=document.forms["booking"]["email"].value;
if (x.indexOf("@")=== -1)
{
alert("Please enter a valid email");
return false;
}
}
I can't really change the structure of this, so it needs to fit into this bit of code :( any suggestions or answers is greatly appeciated :)
我无法真正改变它的结构,所以它需要适应这段代码:(任何建议或答案都非常受欢迎:)
回答by phihag
Use regexp.test
:
使用regexp.test
:
if (! /^[0-9]{11}$/.test(x)) {
alert("Please input exactly 11 numbers!");
return false;
}
Note that you're intentionally excluding a lot of phone numbers, including international ones or alternative writing styles of phone numbers you could reach. You should really just test for /^\+?[0-9]+$/
.
请注意,您有意排除了许多电话号码,包括国际电话号码或您可以联系到的其他电话号码书写风格。你真的应该只测试/^\+?[0-9]+$/
.
回答by Cheery
if (!/^\d{11}$/.test(x))
alert('These are not 11 digits!');
else
alert('Passed!');
Try it http://jsfiddle.net/LKAPN/1/
回答by knub
Use regex:
使用正则表达式:
"1234".match(/^\d{4}$/);
Explanation:
解释:
^ - matches the beginning of the string
\d - matches one digit
{4} - tells that we want four digits (you want thirteen then)
$ - matches the end of the string
See https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressionsfor further reading.
如需进一步阅读,请参阅https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions。
回答by JKing
kinda hacky, but you could do:
有点hacky,但你可以这样做:
if(x.length==11)
{
for (var i=0; i<x.length; i++)
{
if (parseInt(x[i])!=x[i])
{
alert("only enter numbers!");
}
}
}
else
{
alert("must enter 11 digits!");
}
回答by cdm9002
You can the use regular expression:
您可以使用正则表达式:
\d{11}
However, you probably also want to strip whitespace:
但是,您可能还想去除空格:
var isElevenNumbers = (/^\d{11}$/m.test(mynumber.replace(/\s]/g, '')));
And you may want to strip more for internation numbers, e.g. +1-234-567-9090
并且您可能想要去除更多的国际号码,例如 +1-234-567-9090
var isElevenNumbers = (/^\d{11}$/m.test(mynumber.replace(/[\s\+\-]/g, '')));
where 'mynumber' is your variable holding the value.
其中 'mynumber' 是您保存值的变量。
回答by Blockpusher
Steven Levithan has a very useful excerpt on Regexps that validate North American as well as international phone numbers at http://blog.stevenlevithan.com/archives/validate-phone-number.
Steven Levithan 在http://blog.stevenlevithan.com/archives/validate-phone-number上有一个关于验证北美和国际电话号码的正则表达式的非常有用的摘录。
That page is excerpted from his book Regular Expressions Cookbook(O'Reilly, 2009)
该页面摘自他的书正则表达式食谱(O'Reilly,2009)
回答by Petah
You can use regex for that:
您可以为此使用正则表达式:
var str = '12345678900';
if (str.match(/^\d{11}$/)) {
// Valid
}
回答by James McLaughlin
Maybe something like:
也许是这样的:
if (x.length == 11 && +x == x)