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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:02:36  来源:igfitidea点击:

regex to allow only numbers and single dot in jquery

javascriptjqueryregex

提问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+\.$

回答by Michael Durrant

I would try using the following:

我会尝试使用以下内容:

\d+\.\d?{2}

http://rubular.com/r/a83BWznDuy

http://rubular.com/r/a83BWznDuy