Regex for only positive values?

Hi all!

I have a text field where I'm asking the user to provide a number that needs to be a positive value. What is the constraint/regex text for enabling this? Can't seem to find much guidance out there!

Thank you!

You could skip regex and just use a simple test, i.e.


Edit, just noticed you said text field! Is there a reason you can't use integer or decimal type? If so, you may have to convert the text to a number and then test it.

Otherwise you certainly could write regex to ensure only digits (and a decimal separator) are entered.

Thanks for the quick reply! I was using text as have got used to that over the years with regex constraints. For the .>0 this would go in the constraint column?

I tested a few cases and it seems fairly tolerant of type.

text with constraint .>0 allows alphanumeric input but only positive numbers entered numerically are valid
text with constraint number(.)>0 allows alphanumeric input but only positive numbers entered numerically are valid
integer with constraint .>0 allows numeric (but no .) input but only positive integers are valid
decimal with constraint .>0 allows numeric input but only positive numbers are valid

1 Like

Correct. XPath expression evaluation will dynamically type-cast operator arguments (and often XPath function arguments too) as needed. It doesnt actually know what the associated XForm binding type might happen to be (!), so it's all based off the XPath expression itself.

That said, personally, especially when it comes to dates, I still prefer to uses things like number() and decimal-date-time() to explicitly indicate the conversion taking place.

[oh, but this type-casting magic isnt smart enough to know to convert "two" to 2, so as @ahblake suggests, if you are really wanting the user to enter a number then I'd agree you should probably make the question integer or decimal, not text].