Reputation: 328
I am trying to write regex which allow only number between 1-18 with decimal value. Below expression allow 18.10 - 18.99 .Please suggest if any. Thanks in advance
((?:[1-9]|1[0-8])(?:\.\d{1,2})?|18(?:\.00?)?)$
Upvotes: 2
Views: 98
Reputation: 147266
Your regex is basically fine, you just need to change [0-8]
to [0-7]
to not allow any values starting with 18
other than 18.0
or 18.00
, and you should escape the .
characters to force them to match only a .
. Finally you should anchor the match to the beginning of the string as well as the end:
^((?:[1-9]|1[0-7])(?:\.\d{1,2})?|18(?:\.00?)?)$
Demo on regex101
Demo on 3v4l.org
Upvotes: 4