javascript 正则表达式只允许在 jquery 中使用数字和单点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17086774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
regex to allow only numbers and single dot in jquery
提问by R J.
Regex to allow only numbers and single dot when entering number to textbox in jquery.
在 jquery 中将数字输入到文本框时,正则表达式只允许数字和单点。
Please suggest any regex to allow only numbers and single dot in textbox.
请建议任何正则表达式只允许文本框中的数字和单点。
I have tried the following code.
我试过下面的代码。
$("#amountId").val().replace( /[^0-9]+/g, '')
回答by ahPo
[-+]?([0-9]*\.[0-9]+|[0-9]+)
.
[-+]?([0-9]*\.[0-9]+|[0-9]+)
.
See Matching Floating Point Numbers With Regex
请参阅使用正则表达式匹配浮点数
回答by alex
This will find something like this 1234.5678
/\d+\.\d+/
这会找到这样的东西 1234.5678
/\d+\.\d+/
回答by Mike Perrenoud
This regex should do the trick for you, \d+\.$
, and here is a Rubular to prove it. You could even consider prefacing the string with the ^
so that nothing can come before the digits too, like this, ^\d+\.$
.
这个正则表达式应该对你有用\d+\.$
,这里有一个Rubular 来证明它。您甚至可以考虑在字符串^
前面加上 ,这样数字之前也不会出现任何内容,例如,^\d+\.$
。