I have one text input.
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.
function allowOneDot(txt) { if ((txt. value. split("."). length) > 1) { //here, It will return false; if the user type another "." } }
(dot) metacharacter, and can match any single character (letter, digit, whitespace, everything). You may notice that this actually overrides the matching of the period character, so in order to specifically match a period, you need to escape the dot by using a slash \.
Flag u enables the support of Unicode in regular expressions. That means two things: Characters of 4 bytes are handled correctly: as a single character, not two 2-byte characters. Unicode properties can be used in the search: \p{…} .
I think you mean this,
^-?\d+(?:\.\d+)?$
DEMO
It allows positive and negative numbers with or without decimal points.
EXplanation:
^
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With