正则表达式在文本框中仅允许一个点 [英] Regex to allow only a single dot in a textbox
本文介绍了正则表达式在文本框中仅允许一个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只有一个文字输入.
我写了一个正则表达式来屏蔽除.
和-
之外的所有特殊字符.现在,如果用户错误地在输入中输入了两个.
(点),则使用当前的正则表达式
I wrote a regex for masking all special characters except .
and -
. Now if by mistake the user enters two .
(dots) in input, then with the current regex
var valueTest='225..36'
valueTest.match(/[^-.\d]/)
我希望电话号码不会通过此条件
I expected that the number will not pass this condition
如何处理这种情况.我只想在输入字段中输入一个 .
(点),因为它是一个数字.
How to handle this case. I just want one .
(dot) in input field since it is a number.
推荐答案
我认为你的意思是
^-?\d+(?:\.\d+)?$
它允许带或不带小数点的正数和负数.
It allows positive and negative numbers with or without decimal points.
预期:
-
^
断言我们处于起步阶段. -
-?
可选的-
符号. -
\d+
匹配一个或多个数字. -
(?:
非捕获组的开始. -
\.
匹配文字点. -
\d+
匹配一个或多个数字. -
?
将整个非捕获组设为可选. -
$
断言我们已经结束了.
^
Asserts that we are at the start.-?
Optional-
symbol.\d+
Matches one or more numbers.(?:
start of non-capturing group.\.
Matches a literal dot.\d+
Matches one or more numbers.?
Makes the whole non-capturing group as optional.$
Asserts that we are at the end.
这篇关于正则表达式在文本框中仅允许一个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文