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>
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>
Share
Improve this question
edited Sep 6, 2019 at 9:20
Brian Tompsett - 汤莱恩
5,89372 gold badges61 silver badges133 bronze badges
asked Sep 24, 2013 at 16:27
Horace HeavenHorace Heaven
7105 gold badges14 silver badges24 bronze badges
4
- 2 What is wrong with the code you've provided? You may be looking for something similar to this: stackoverflow./a/15556249/1266600 – sushain97 Commented Sep 24, 2013 at 16:35
- The link to the solution you posted works fine for accepting integer only values but it does not accept floating point values for e.g. 3.14 – Horace Heaven Commented Oct 7, 2013 at 4:48
- You need to modify it to fit your pattern. – sushain97 Commented Oct 7, 2013 at 13:43
- See this man : stackoverflow./a/20368741/2837412 – Nishchit Commented Dec 4, 2013 at 6:42
2 Answers
Reset to default 5If you are asking about correct "float/integers" regex pattern then try this one :
/^\-?\d+((\.|\,)\d+)?$/
An ideal idea for decimal numbers and setting the decimal region to 2
ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/"