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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745551921a4632615.html
评论列表(0条)