Problem in Implementation of Regular Expression: ODK2.0

I wanted to validate 3 digit numbers with regex(.,’^[0-9]{3}$’). But throws error while converting through application designer.
“Error: Error: Unexpected token . interpretting formula: regex(.,’^[0-9]{3}$’) on sheet: survey row: 17 column: constraint”
Then after I changed regex to regex(’.’,’^[0-9]{3}$’) it converted successfully.
But when I tried to preview the survey screen it throws error: Exception while evaluating constraint() expression. See console log.
The console log shows:
E/Exception: ‘regex is not defined’ in user-defined expression: regex(’.’,’^[0-9]{3}$’) on survey row 17 column: constraint.
Does anyone have experience with it.
demogForm.xlsx (17.6 KB)

Hi @nsthakuri!

See: Problem adding regex constraint using the application designer

Short answer: Regex is not part of ODK2 (it’s ODK1).


Hi @nsthakuri,

Actually, there is a way to use a regex in ODK 2 for your constraints using the JavaScript test() function. Your constraint would be the following.


Thank you for providing your xlsx file. I have modified it to use the constraint and attached it. demogForm.xlsx (17.6 KB)

Let me know if you have other issues.


Thank you clarice_larson for taking time to this issue which is helpful and really worked for me.
Also thanks elmps2018,
Now I understood that regex doesn’t work for ODK2.0. Thanks for your help.

Hi @clarice_larson,
Is it possible to validate the range from 001-160 using this pattern. (/^0[0-9][1-9]|1[0-5][0-9]|160$/).test(data(‘myid’)).
Thanks in advance.

I got the solution.
should work.