The regex function does not work in my form

Indeed, I want to limit certain value entry to 2 digits for certain, and 09 digits for telephone numbers. I followed the method, but I do not get convincing results. What is the problem? I attach the form with the parties concernedTest 7.xlsx (23.6 KB)

Hi @esy_cari2020,

Regarding your first query:

Your xlsform wishes to restrict to 5 but you have stated 2 here. However you could do this as follows:

To restrict an entry to 5 digits:
regex(., '^[0-9]{5}$')

To restrict an entry to 2 digits:
regex(., '^[0-9]{2}$')

Regarding your second query:

Your xlsform wishes to restrict to 8 but you have stated 9 here. However you could do this as follows:

To restrict telephone number entry to 8 digits:
regex(., '^[0-9]{8}$')

To restrict telephone number entry to 9 digits:
regex(., '^[0-9]{9}$')

Please also see the images below for detailed instruction:

In the survey tab of your xlsform:

Note: Please see the changes that is within the red box as shown in the image above.

Data entry screen as seen in Enketo: When entered correctly

Data entry screen as seen in Enketo: When entered wrongly

Reference xlsform:
Test 7.xlsx (10.1 KB)

You could also learn more on regex by following our support article Restricting text responses with regular expressions.

Have a great day!

Hello,
just adding to Kal_Lam: Be very careful
KoBoCollect android app and Enketo web forms behaves a bit differently with regex. KoBoCollect android app behaves as if you have used the anchors (^ $) around the regex (even if you have not used them), while Enketo requires the anchors as mandatory for an exact match. (see support article)
See also: Regex Problem in KoBoCollect
Kind regards

2 Likes

Hi Kal_Lam,
How can I restrict my response to exact 17 digit. It is an integer type. I have put the function like this:
regex(.,’^[0-9]{17}$’)
But, it won’t allow me to input more than 9 digit. How can I solve it?

Hello,
Integer type in KoBo is generally limited to 9 digits.

@tasfik_rukan, your question type should be text, not integer. It should then work. You will also need to change the appearance to numbers.