Problem in Implementation of Regular Expression: ODK2.0

Hi,
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.
Thanks,
demogForm.xlsx (17.6 KB)

Hi @nsthakuri!

See: Problem adding regex constraint using the application designer - #2 by elmps2018

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

Best,
Caroline

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.

(/^[0-9]{3}$/).test(data(‘hh’))

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.

Clarice

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.
Thanks.

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.
(/^(160|0[0-9][1-9]|1[0-5][0-9])$/).test(data(‘myid’))
should work.