Search code examples
angularjsng-pattern

ng-pattern to allow numbers from certain range before and after decimal


How to limit numbers for before and after the decimal point, something like before decimal should be from 0-30 and after decimal should be from 0-15

Correct: 30.15
Wrong: 30.16
Correct: 30.09 or 30.9
Correct: 0.15
Wrong: 0.16
Correct: 21.14
Wrong: 21.56

Looking for a regEx.


Solution

  • How about this for a solution? I know you said RegEx, but this could do the trick as well.

    $scope.check = function(value) {
    
      console.log(value);
      var split = value.split('.');
    
      var part1Ok = split[0] <= 30;
      var part2Ok = split[1] <= 15;
    
      $scope.result = part1Ok && part2Ok;
    }
    

    http://plnkr.co/edit/nzTNdOR1eWZcwH5zYvIk?p=preview