Regex for text entry of number between 0 and 2, using "." as separator | XM Community
Skip to main content
Hi,



I would like to use a "Matches Regex" custom validation to ensure that a text entry is a number between 0 and 2. Decimals should be allowed (but not necessary), but they have to use "." (not ",") as a separator.



I have tried "^[0-2]+.[0-9]$", but there are two problems: 1) the code allows "," as a separator and 2) it does not allow a number without a decimal.



I am new to Regex, so any leads would be much appreciated!
Without having tested it: that decimal point is a regex metacharacter. So you likely need to escape it. Try: "^[0-2]+\\.[0-9]$"
Thanks for your answer! Unfortunately, I have already tried what you are suggesting and there are two problems: 1) the code allows "," as a separator and 2) it does not allow a number without a decimal.
Alright- try the following:



```(^[0-2])|(^[0-2]\\.[0-9]$)```



The only thing I don't like about this one, is it does force a zero. So you can't type ".5" you have to type "0.5". But ignorning that hiccup, all else works as expected 🙂
Thanks a lot!

Leave a Reply