I’ve googled for literally hours and I’m amazed I haven’t found an answer to this.
Using the regex regex(., ‘^[0-9]{3}$’) I want to limit user input to a number between 000 and 999, however I want input with one or two leading zeros to be possible (e.g. 010 or 001).
This above code will not allow one or two leading zeros!
I tried adapting this code for my needs and the same error occurs. I then uploaded your original regex and it didn’t work either; the regex doesn’t allow 01 to 09.
Hello,
So, it’s just because you work with leading zeros for this ´number field?
Using a text field for a number, can create disadvantages for calculations, aggregations (like mean) and range constraints…
@Kal_Lam:
Would a regex work for an integer field (no leading zeros)?
For an integer field, can a type-cast be used, like regex( string(.), …)
When you try to use the workaround shared above with an integer question type, the regex code does not seem to function as it should. This is why @sjed1 was having an issue. After changing the same from integer to text question type and using numbers as appearance it should work.
Regarding your other issue …
Could you kindly share with us a sample of a dummy xlsform so that we could further explore and see.
Hello @Kal_Lam,
“… with an integer question type, the regex code does not seem to function as it should.”
So, would you mind to create a github Bug entry, please?
Thanks in advance.
Kind regards
Int datatypes aren’t stored with leading zero’s in XForms, which is probably why they’re not getting picked up and detected by the subsequent regex checker. Trying using a number text question instead, which is actually a proper string data type that will persist your zeros in situ.
The canonical representation for int is defined by prohibiting certain options from the Lexical representation (§3.3.17.1). Specifically, the the optional “+” sign is prohibited and leading zeroes are prohibited.
Hello,
I think the regex syntax should be
regex(., ‘^[0-9]{3}$’) and (. >= 1).
The syntax form of your example works, but is not the normal regex style
[001-999]{3} doesn’t mean a (grouped) sequence “001” … to “999” (repeated 3 times). Instead it means that the single characters 0, 1 … 9 are allowed, with a total of 3 times/characters.
Also, as far as I tested, the >= 001 is internally converted to >= 1. (You may try with >= 000001 e.g.)
Side-note: IDs might be designed with starting at 1nn, like 100 (or 101)… - 999. So, integer type could be used without having leading zero issues.
Hello,
Just to add: The GUI in Enketo and KoBo/ODK Collect allows to enter leading zeros for integer (and decimal) types.
(Internally removed/converted for a regex test.)
The leading zeros stay on the screen at first and will be removed
in Enketo only on save (neither on field move (nor on validation in Preview)
in Collect already on moving out of the field (next) or when using the up/down number button.
Kind regards