Leading Zeros Not Accepted in Regex constraint

Hi all,

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!

Thanks!

Maybe this post discussed previously should solve your issue:

1 Like

Hi Kal,

Thank you for the reply.

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.

I wonder if this is a bug?

Have you tried this:

In the survey tab of your xlsform:

It should solve your issue (i.e. users are only allowed to enter 3 digits from 001 to 999).

Reference xlsform:

Regex_Constraint.xlsx (10.0 KB)

If you are still facing an issue, please share with us a screenshot of the issue. The community would love to help you solve your issue.

Note: if you wish to have the numbers between 000 to 999 you could simply change the regex code to (regex(., '^[001-999]{3}$') and (. >= 000))

2 Likes

It works!

I had the question type as integer and that was the problem.

Once set as text, it works.

Thank you!

2 Likes

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(.), …)

Kind regards

1 Like

Hi @wroos,

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

1 Like

Hi @wroos,

I am looping @Xiphware here so that he could share his thoughts on this.

2 Likes

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.

[for the gory details, see https://www.w3.org/TR/xmlschema-2/#int. Specifically (emphasis added):

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.

3 Likes

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.

Kind regards

2 Likes

You got that correct! Thank you @wroos for updating :clap:

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
1 Like