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

Regex for text entry of number between 0 and 2, using "." as separator

  • July 24, 2019
  • 4 replies
  • 86 views

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!

Best answer by Kate

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 :)

4 replies

Forum|alt.badge.img+19
  • Level 5 ●●●●●
  • July 24, 2019
Without having tested it: that decimal point is a regex metacharacter. So you likely need to escape it. Try: "^[0-2]+\\.[0-9]$"

  • Author
  • July 25, 2019
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.

Forum|alt.badge.img+19
  • Level 5 ●●●●●
  • Answer
  • July 29, 2019
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 :)

  • Author
  • August 2, 2019
Thanks a lot!