Search code examples
angularjs-ng-pattern

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


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.


Solution

  • 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.