How to allow only one decimal point for input of type number in ionic 1

Viewed 5765

I am using a input of type number in which it allows multiple decimal points so i have tried using the regex not to allow more than one decimal point but even after using the regex i am facing the same issue can anyone tell me how to allow only one decimal point in input of type number in ionic1

Html:

<input stopccp focus-me class="inputContainer trade_input" type="number" name="" ng-model="vm.total_amount[$index]" ng-change="vm.onTotalCost()" limit-char limit="5" ng-keyup="vm.decimalcheck(vm.total_amount[$index])"  >

Regex in function:

function decimalcheck (element) {
  $log.log('decimalcheck got called', element);
  var regex = /\d*\.?\d?/g;
  return regex.exec(element);
}
6 Answers

Try this in your html, It worked for me

pattern = "^\d*\.?\d+$"

like this:

<input stopccp focus-me class="inputContainer trade_input" type="number" name="" ng-model="vm.total_amount[$index]" ng-change="vm.onTotalCost()" limit-char limit="5" ng-keyup="vm.decimalcheck(vm.total_amount[$index])" pattern="^\d*\.?\d+$">

this work with input field use pattern and event onInput

<input
   maxLength={4}
   type="text"
   pattern="\d+\.?\d?(?!\d)" // [0-9]*\.?[0-9]*
   value={unitProgress}
   onInput={(e: any) => {
      const valueChange = e.target.validity.valid 
                             ? e.target.value 
                             : unitProgress;
      setUnitProgress(valueChange);

  }}
                                       
/>
Related