validate natural input number with ngpattern

前端 未结 3 975
悲&欢浪女
悲&欢浪女 2020-12-01 07:22

I use ng-pattern=\"/0-9/\" to set price_field do not accept decimal number. But when I input natural number (from 0 to 9999999),

3条回答
  •  隐瞒了意图╮
    2020-12-01 07:51

    The problem is that your REGX pattern will only match the input "0-9".

    To meet your requirement (0-9999999), you should rewrite your regx pattern:

    ng-pattern="/^[0-9]{1,7}$/"
    

    My example:

    HTML:

    Not a valid number! This field is required!

    JS:

    function formCtrl($scope){
      $scope.onSubmit = function(){
        alert("form submitted");
      }
    }
    

    Here is a jsFiddle demo.

提交回复
热议问题