Reputation: 3
I need a regular expression that validates a decimal number, but doesn't require any symbol befor number . ie.
.2 (fine)
.12 (fine)
12 (fine)
12.0 (fine)
12.12 (fine)
But
12.123 (not allow)
-12.12(not allow)
+12.12 (not allow)
Upvotes: 0
Views: 67
Reputation: 12036
[0 to 9 as many times as it is possible or none] dot [0 to 9 1 or times]
/^([0-9]+)?(\.[0-9]{1,2})?$/
Edit: added starting and ending limiters.
Upvotes: 1
Reputation: 660
Just to add to this solution there are multiple online REGEX debuggers. One of my favorite is: http://www.debuggex.com/
It will allow you to put in language, REGEX, and sample data.
Upvotes: 0
Reputation: 321
The original answer requires numbers before the decimal and numbers after it. after the decimal.
Hooray for edits.
([0-9]+)?(\.[0-9]{1,2})?
Upvotes: 1