简体   繁体   English

正则表达式使用JavaScript在JSP中仅允许使用0-9位数(甚至不包括'。'[Dot])

[英]Regular Expression to allow only 0-9 digits only (Not even '.' [Dot]) in JSP using javascript

Could anyone please provide me with a regular expression to allow only digits (0-9) excluding even '.' 谁能为我提供一个正则表达式,以允许仅输入数字(0-9),甚至不包括“。”。 (decimal point) in a textbox in JSP using javascript. (小数点)使用JavaScript在JSP中的文本框中。 I would be using it to replace the resricted characters with '' (empty string). 我会用它用''(空字符串)替换限制的字符。

I tried few but they are not restricitng the DOT. 我尝试了几次,但他们没有限制DOT。

Thanks in advance 提前致谢

正则表达式模式为:

/^[0-9]+$/

You need to anchor the regex: 您需要锚定正则表达式:

/^\d*$/

to make sure that the entire string consists of digits. 以确保整个字符串由数字组成。

Without ^ and $ , the regex would match 1 (and 234 ) in 1.234 . 如果没有^$ ,则正则表达式将匹配1.234中的1 (和234 )。

I would use the class \\d , it includes only digits. 我将使用类\\d ,它仅包含数字。 If you want to replace all non-digits you would need to replace \\D+ with "" . 如果要替换所有非数字,则需要将\\D+替换为""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM