Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether text field contains numbers only and has to be 11 digits

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

like image 882
Richlewis Avatar asked Dec 12 '25 13:12

Richlewis


1 Answers

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

like image 165
burning_LEGION Avatar answered Dec 15 '25 06:12

burning_LEGION



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!