Avinash Gupta
Avinash Gupta

Reputation: 328

Regex to allow number between 1-18 with decimal values

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

Answers (1)

Nick
Nick

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

Related Questions