Regex to only allow numbers under 10 digits?

前端 未结 7 1669
暖寄归人
暖寄归人 2020-12-16 22:06

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 t

相关标签:
7条回答
  • 2020-12-16 22:12

    Use this regular expression to match ten digits only:

    @"^\d{10}$"
    

    To find a sequence of ten consecutive digits anywhere in a string, use:

    @"\d{10}"
    

    Note that this will also find the first 10 digits of an 11 digit number. To search anywhere in the string for exactly 10 consecutive digits.

    @"(?<!\d)\d{10}(?!\d)"
    
    0 讨论(0)
  • 2020-12-16 22:13

    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}$
    
    0 讨论(0)
  • 2020-12-16 22:25

    Would you need a regular expression?

    var value = +$('#targetMe').val();
    if (value && value<9999999999) { /*etc.*/ }
    
    0 讨论(0)
  • 2020-12-16 22:31

    check this site here you can learn JS Regular Expiration. How to create this?

    https://www.regextester.com/99401

    0 讨论(0)
  • 2020-12-16 22:34
      var reg      = /^[0-9]{1,10}$/;
      var checking = reg.test($('#number').val()); 
    
      if(checking){
        return number;
      }else{
        return false;
      }
    
    0 讨论(0)
  • 2020-12-16 22:35
    var value = $('#targetMe').val(),
        re    = /^[1-9][0-9]{0,8}$/;
    
    if (re.test(value)) {
        // ok
    }
    
    0 讨论(0)
提交回复
热议问题