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
use 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With