Skip to content Skip to sidebar Skip to footer

Regex To Only Allow Numbers Under 10 Digits?

I'm trying to write a regex to verify that an input is a pure, positive whole number (up to 10 digits, but I'm applying that logic elsewhere). Right now, this is the regex that I'm

Solution 1:

You can do this way:

/^[0-9]{1,10}$/

Code:

var tempVal = $('#targetMe').val();
if (/^[0-9]{1,10}$/.test(+tempVal)) // OR if (/^[0-9]{1,10}$/.test(+tempVal) && tempVal.length<=10) 
  alert('we cool');
else
  alert('we not');

Refer LIVE DEMO


Solution 2:

var value = $('#targetMe').val(),
    re    = /^[1-9][0-9]{0,8}$/;

if (re.test(value)) {
    // ok
}

Solution 3:

Would you need a regular expression?

var value = +$('#targetMe').val();
if (value && value<9999999999) { /*etc.*/ }

Solution 4:

  var reg      = /^[0-9]{1,10}$/;
  var checking = reg.test($('#number').val()); 

  if(checking){
    return number;
  }else{
    return false;
  }

Solution 5:

That's the problem with blindly copying code. The regex you copied is for numbers including floating point numbers with an arbitrary number of digits - and it is buggy, because it wouldn't allow the digit 0 before the decimal point.

You want the following regex:

^[1-9][0-9]{0,9}$

Post a Comment for "Regex To Only Allow Numbers Under 10 Digits?"