javascript - Accepting only integers and floating point numbers in angular js on each key stroke - Stack Overflow

How can angular js monitor key strokes in such a way that the input field in html does not accept non n

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
Add a ment  | 

2 Answers 2

Reset to default 5

If 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})?$/"  

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744144466a4560355.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信