I'm having a hard time coming up with the most suitable regex for my needs.
I want a regex that only validates numbers from 1-8, with or without a decimal point
1 // Valid
3 // Valid
8 // Valid
2.00 // Valid
4.45 // Valid
7.60 // Valid
9 // Invalid
10 // Invalid
9.00 // Invalid
50.40 // invalid
So far this is what I came up with, but ^([0-8]$\.?[0-8]*|\.[0-8]+) but this only accepts numbers from 1-8, and nothing with a decimal. Can someone kindly provide a suitable regex for this example?
I guess it would be better to limit regex to string parsing. My best approach would be to parse a valid number from the string and then convert it to number before doing any validation on its value as being < x
But if you really want to go via regex (and it will be hard to change in case) would be with something like:
^0*([0-7](\.\d+)?|8)$
It matches any number between 0 and 8 ... decimal point is allowed if followed by numbers and the highest valid number like that would be 7.999999999... followed by the next valid integer number being 8.
Trailing zeros are considered.