ng-pattern to check amount should be of 1 to 3 digit with 2 decimal value (999.99)?

714 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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.

1
On

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" />