最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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})?$/"  
发布评论

评论列表(0)

  1. 暂无评论