1

I am working on a transaction form. In the form, there is a field name Amount. I am using this ng-pattern i.e. ng-pattern="/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{1,2})?$/" which allow only number with 2 decimal value. Also, 0 is also not eligible for this input pattern.

<input type="text" ng-model="copayAmount" ng-pattern="/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{1,2})?$/" maxlength="9" required> 

Above expression allow 9 digit value with 2 digit decimal value. But I want 3 digit only with 2 decimal value.

I want ng-pattern for only digit should be valid within only 3 digit (not more than 3 digit) with 2 decimal value i.e. 999.99 (0 should not be valid).

Please help. Thanks.

Community
  • 1
  • 1
Nishant
  • 127
  • 12

2 Answers2

0

Your pattern is working, you can check from https://regex101.com/r/XaumOY/1

$scope.numberPattern = (function() {
    var regexp = /^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{1,2})?$/;
    return {
        test: function(value) {
            if( $scope.checkedumber=== false ) {
                return true;
            }
            return regexp.test(value);
        }
    };
})();

And in HTML no changes would be required:

<input type="text" ng-model="..." ng-required="checkedumber"
    ng-pattern="numberPattern" />
Dr. X
  • 2,890
  • 2
  • 15
  • 37
  • @Greek my pattern is correct with all amount. But I want user can add amount which value should not more than 3 digit i.e. 9999 (invalid). I want following value i.e. 1 & more than 1 or 9.99, 99.99 & 999.99. I want only these pattern. I don't want 9999.99. Please help. – Nishant Sep 07 '17 at 04:08
0

Guys I have implement the pattern for this case :

ng-pattern="/^[1-9][0-9]{0,2}(\d{3}[-.]?:,?[0-9]{3}){0,3}(?:\.[0-9]{1,2})?$/"

This pattern allow digit more than 0 with 1 to 3 digit & 2 decimal value.

Nishant
  • 127
  • 12