I have searched a lot also tried but it is not working for me. I am using java script.
Asked
Active
Viewed 46 times
-3
-
1Share your code. What have you tried? – Fengson Apr 13 '16 at 10:02
-
update your question with code. what regular expression you are trying? – kudlatiger Apr 13 '16 at 10:03
-
Are the boundaries `0.00` and `20.00` *included* or *excluded*? Is `1.9999999999` a valid input or you require *exactly two* digits after decimal point? Could, please, you provide *some examples*? – Dmitry Bychenko Apr 13 '16 at 10:05
-
using regular expressions for validating numeric ranges is not recommended. http://stackoverflow.com/questions/22130429/using-regular-expressions-to-validate-a-numeric-range – kudlatiger Apr 13 '16 at 10:05
-
I require only exactly 2 digits after decimal point – Bikram Sahu Apr 13 '16 at 10:08
-
@Bikram Sahu: and what about *leading zero*? E.g. `05.00` is it a valid input? – Dmitry Bychenko Apr 13 '16 at 10:16
1 Answers
1
Providing that
- Both borders are included (so
0.00
and20.00
are valid) - Two digits after the decimal point are mandatory (
5
in invalid, but5.00
is OK) - Leading zeros are invalid (so
05.00
is invalid)
the regular expression can be
(^1?[0-9]\.[0-9]{2}$)|(^20\.00$)
Edit: if requests are
- Both borders are included (so
0.00
and20.00
are valid) - There're at most two digits after the decimal point(
5
,5.1
,5.12
are valid, but5.123
is not) - Dangling dots are not allowed (
5.
is not valid) - Leading zeros are invalid (so
05.00
is invalid)
the regular expression can be
(^1?[0-9](\.[0-9]{1,2})?$)|(^20(\.0{1,2})?$)

Dmitry Bychenko
- 180,369
- 20
- 160
- 215
-
No 5 is also valid like it should handle both the scenario 5 & 5.00 or 5.89 etc – Bikram Sahu Apr 13 '16 at 10:23
-
1@Bikram Sahu: But you've put "I require only **exactly 2 digits** after decimal point". So what about `5.1` is it a valid input? And what about `5.` (note the dot, please) – Dmitry Bychenko Apr 13 '16 at 10:27
-
-
`^(1?\d(?:\.\d{1,2})?|20(?:\.00?)?)$` would be a bit simpler don't you think? – Thomas Ayoub Apr 30 '16 at 14:44
-
@Thomas: `\d` allows *any* digit, including, say, Persian ones: `۰ ۱ ۲ ۳ ۴ ۵ ۶`; when `[0-9]` matches exactly `0..9` range – Dmitry Bychenko May 04 '16 at 06:30
-