Setting a default answer (editable, text type of question) based on a select_one question

1. What is the problem? Be very detailed.
I am trying to create a household ID based on the first three letters of a district, and then add two digits (if the district name is papine for instance, I'd like the household ID to be PAP01 etc).
2. What app or server are you using and on what device and operating system? Include version numbers.
I am using ODK collect, XLS form. Galaxy tabA, OS 5.0
3. What you have you tried to fix the problem?
Nothing really. I am thinking perhaps regex combined with a calculate function might be the solution? I don't know how to go about that though.

4. What steps can we take to reproduce the problem?
I have atached a sample form with an explanation on what I have in mind.
5. Anything else we should know or have? If you have a test form or screenshots or logs, attach below.
useprs_trial.xlsx (15.7 KB)

This sample has two options for you to consider. For either, I recommend using the district code as the underlying value instead of numbers. Not only will that code be directly usable to build household ids but it should also make analysis clearer.

The first example does something like what you described. When the district question is set, it replaces the householdid value with the district code. This is done by having a calculation of ${district} and also a trigger of ${district} so that the value is only replaced if the district changes (see documentation on dynamic defaults from form data). The householdid question has a constraint that forces it to be the district code followed by two digits:
substr(., 0, 3) = ${district} and regex(., '^[a-zA-Z]{3}[0-9]{2}$').

The second example is simpler: it prompts the user for the household number, generates the householdid, and shows the generated id to the user for confirmation.

1 Like

@LN thank you very much!

1 Like