1

How can angular js monitor key strokes in such a way that the input field in html does not accept non numerical values and accept only integers and floating point numbers on each key stroke?

<form name="myForm">
  <input ng-pattern="/^(?=.+)(?:[1-9]\d*|0)?(?:\.\d+)?$/" ng-model="value" name="number"/>
  Valid? {{myForm.number.$valid}}
</form>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Horace Heaven
  • 712
  • 5
  • 13
  • 24

2 Answers2

5

If you are asking about correct "float/integers" regex pattern then try this one :

/^\-?\d+((\.|\,)\d+)?$/
Dharmesh Porwal
  • 1,406
  • 2
  • 12
  • 21
0

An ideal idea for decimal numbers and setting the decimal region to 2

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