how to specify a decimal (dd,dd) with type text usign ng-pattern

65 Views Asked by At

I hope my question is clear, what i want to do is validating a decimal with 2 numbers max in the entry part and 2 numbers max in the decimal part (example 99,99) and i want it to accept 100 as max.

I can't use type="number" with min and max because Internet Explorer dose not accept comma in this type which i want to use, so here is my code:

<input type="text" class="form-control" name="fiscal" id="fiscal"
                    ng-model="vm.peticion.aportacion.benif_fiscal" ng-maxlength="5" placeholder="Escribir cantidad ..."
                    ng-style="(form.fiscal.$invalid) && ( form.fiscal.$touched || submitted) && {'background-color':'pink'}"
                    ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/"/>
                    <span ng-style="{'color':'red','font-size':'12px'}" ng-show="form.fiscal.$error.pattern && ( form.fiscal.$touched || submitted)">
                    Beneficio fiscal not valid!
                   </span>

what i'm looking for is the right pattern to do that, this one allows more than 100.

I want the acceptable values to be : from 00,00 to 99,99 and 100 using (,) or (.) and it should accept numbers without decimal part like 22

Thank youuuu !

2

There are 2 best solutions below

2
On BEST ANSWER

Try using this,

ng-pattern="/^([1-9]|([0-9][0-9]))(,|\.)([1-9]|([0-9][0-9]))|^(\d{1,2}(?!\d)|100)$/"
0
On

Can do like this ng-pattern="/(^[0-9]{1,2})+(\.[0-9]{1,2})?$/"

<form name="myForm">
    <div>
      <input type="text" ng-pattern="/(^[0-9]{1,2})+(\.[0-9]{1,2})?$/" name="deci" class="form-control oditek-form" placeholder="Add Latitude coordinate" ng-model="latitude" ng-keypress="clearField('businessno');" >
    </div>
    <label ng-show="myForm.deci.$error.pattern">Error</label>
  </form>