Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
80
rated 0 times [  86] [ 6]  / answers: 1 / hits: 20411  / 10 Years ago, thu, july 31, 2014, 12:00:00

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.


More From » jquery

 Answers
10

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.


[#69983] Monday, July 28, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
travion

Total Points: 137
Total Questions: 96
Total Answers: 103

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
travion questions
Mon, Dec 16, 19, 00:00, 5 Years ago
Sat, Oct 19, 19, 00:00, 5 Years ago
Fri, Sep 20, 19, 00:00, 5 Years ago
Wed, Nov 14, 18, 00:00, 6 Years ago
Sun, Oct 28, 18, 00:00, 6 Years ago
;