admin管理员组文章数量:1202815
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)
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 :)
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)
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 :)
Share Improve this question edited Feb 8, 2012 at 0:59 phihag 288k75 gold badges467 silver badges483 bronze badges asked Feb 8, 2012 at 0:39 RabianiRabiani 1432 gold badges5 silver badges12 bronze badges 1- YOU ARE ALL AMAZING! THANK YOU! – Rabiani Commented Feb 8, 2012 at 0:46
8 Answers
Reset to default 13Use 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]+$/
.
if (!/^\d{11}$/.test(x))
alert('These are not 11 digits!');
else
alert('Passed!');
Try it http://jsfiddle.net/LKAPN/1/
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_Expressions for further reading.
You can use regex for that:
var str = '12345678900';
if (str.match(/^\d{11}$/)) {
// Valid
}
Maybe something like:
if (x.length == 11 && +x == x)
kinda hacky, but you could do:
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!");
}
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
var isElevenNumbers = (/^\d{11}$/m.test(mynumber.replace(/[\s\+\-]/g, '')));
where 'mynumber' is your variable holding the value.
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 .
That page is excerpted from his book Regular Expressions Cookbook (O'Reilly, 2009)
本文标签: validationValidate 11 Digits in JavaScriptStack Overflow
版权声明:本文标题:validation - Validate 11 Digits in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738636181a2104046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论