admin管理员组文章数量:1430956
I have this function to check whether the text field contains numbers only, if there are letters in this field then an error should show
//Tel Validate
function is_valid_tel() {
$this = $("#inputTel");
var pattern = new RegExp("^[0-9]*$");
if(pattern.test($this.val())){ // valid
if ($this.closest(".control-group").hasClass("error"))
$this.closest(".control-group").removeClass("error");
$this.siblings(".help-inline").css("display", "none");
return true;
} else { // error
if (!$this.closest(".control-group").hasClass("error"))
$this.closest(".control-group").addClass("error");
$this.siblings(".help-inline").css("display", "block");
return false;
}
}
When i enter letters into the text field i get no error thrown. My jquery/javascript is limited but I thought this would at least work? as the reg exp checks for numbers between 0-9. I would also like to check that if a number is entered then it matches 11 digits
Any help appreciated thanks
I have this function to check whether the text field contains numbers only, if there are letters in this field then an error should show
//Tel Validate
function is_valid_tel() {
$this = $("#inputTel");
var pattern = new RegExp("^[0-9]*$");
if(pattern.test($this.val())){ // valid
if ($this.closest(".control-group").hasClass("error"))
$this.closest(".control-group").removeClass("error");
$this.siblings(".help-inline").css("display", "none");
return true;
} else { // error
if (!$this.closest(".control-group").hasClass("error"))
$this.closest(".control-group").addClass("error");
$this.siblings(".help-inline").css("display", "block");
return false;
}
}
When i enter letters into the text field i get no error thrown. My jquery/javascript is limited but I thought this would at least work? as the reg exp checks for numbers between 0-9. I would also like to check that if a number is entered then it matches 11 digits
Any help appreciated thanks
Share Improve this question asked Feb 6, 2013 at 9:50 RichlewisRichlewis 15.4k39 gold badges137 silver badges300 bronze badges2 Answers
Reset to default 6use this regular expression ^\d{11}$
old regex
^
begin of string
[0-9]*
0 or more digits
$
end of string
new regex
^
begin of string
\d{11}
11 digits (\d
== [0-9]
)
$
end of string
Try the following RegExp
var pattern = new RegExp("^[0-9]{11}$");
Which is a more readable version of :
var pattern = new RegExp("^\d{11}$");
You will find a handy reference about special characters here.
本文标签: javascriptCheck whether text field contains numbers only and has to be 11 digitsStack Overflow
版权声明:本文标题:javascript - Check whether text field contains numbers only and has to be 11 digits - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745551920a2662981.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论