admin管理员组文章数量:1415100
I need to allow user to only enter integer (33) and float (33.343) in a textbox.
Below is my Regex Query but it is not working fine. It is allowing the user to enter float values but giving error on int values.but i need to allow user to enter int also. It is working fine and giving error message when user enter any other type of value.
/^[0-9]*[.][0-9]*$/
I need to allow user to only enter integer (33) and float (33.343) in a textbox.
Below is my Regex Query but it is not working fine. It is allowing the user to enter float values but giving error on int values.but i need to allow user to enter int also. It is working fine and giving error message when user enter any other type of value.
/^[0-9]*[.][0-9]*$/
Share
Improve this question
edited May 23, 2018 at 15:23
Ωmega
43.7k35 gold badges142 silver badges212 bronze badges
asked Oct 18, 2012 at 12:43
Asp_NewbieAsp_Newbie
2692 gold badges9 silver badges17 bronze badges
0
3 Answers
Reset to default 3Make dot and the part after it optional:
"^\d+(?:\.\d+|)$"
Also prefer \d
over [0-9]
, don't use *
since then you allow the possibility of no number either side of the .
This will accept:
1
1.23
but not:
.1
23.
It's up to you if you want that behaviour.
Try with:
/^[0-9]+(?:\.[0-9]+)?$/
Use regex pattern
^(?=.*\d)\d*(?:\.\d*)?$
This will match for example:
1234
123.
1.23
12.3
0.12
.123
本文标签: javascriptRegular expression for int and float both not working fineStack Overflow
版权声明:本文标题:javascript - Regular expression for int and float both not working fine - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745185216a2646651.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论