Regular expression to constrain answers to match each other for different questions

What is the problem? Please be detailed.
I have a field for surveyors to enter a fieldworker ID. This will be a single letter [A-Z] that is assigned to them. Fieldworkers will be entering a household ID using the following convention: "fieldworker ID-hh#-MMDD". For example, if the fieldworker ID is A and they are visiting the first household of the day on November 18, the household ID should be entered as A-1-1118.

I am trying to use a regular expression to check that the letter entered in the fieldworker ID field matches the first position of the string entered for the household ID and then the rest of of the ID follows the rest of the naming convention.

What ODK tool and version are you using? And on what device and operating system version?
I am using ODK Collect v1.15.1 on a Samsung Galaxy Tab E and building my form with XLSForm.

What steps can we take to reproduce the problem?
Attached is a short sample xlsform that has these 2 question that I am trying to work with (attached).

What you have you tried to fix the problem?
I have tried a number of different regular expressions in the constraint field and either they constrain every possible entry for the hhid (nothing is accepted) or everything is allowed (including incorrect answers).

My current regular expression in the constraint field for the hhid is: regex(.,('^[${surveyorID}][-][0-9]{1}[-][0-9]{4}$'))

Anything else we should know or have? If you have a test form or screenshots or logs, attach here.
regex_example.xlsx (9.2 KB)

1 Like

Hi Nina,

Try the following:

regex(.,('^[A-Z]{1}[-][0-9]{1}[-][0-9]{4}$')) and 
starts-with(., ${surveyorID})

I do not think ODK's regular expressions are powerful enough to allow for such interpolation; that is, the insertion of a variable into the expression.

regex_example.xlsx (9.2 KB)

2 Likes

Works perfectly, thank you!