Given a value, identify the range it is in

Hi All,

Am new in using ODK.
I wanted to validate the Exact Age of a respondent against the below age bands so that the interviewer doesn't select a bracket not in line with the age.

1 Below 18 years
2 18 – 24 years
3 25 – 30 years
4 31 – 34 years
5 35 – 40 years
6 41 – 45 years
Looking forward to your feedback.

Regards

Assuming you are using XLSForm, here's what I'd do...

survey sheet

choices sheet
image

A couple of things to notice:

  • ${age} has to be required or the later comparisons may break. In general, if you use a value later, you should make it required. I've also added a constraint for sensible ages. The period in the constraint means to match the current question.
  • I figure out the ${age_range} automatically in an invisible calculate question.
    • I first compare the age against the known ranges. If the age matches a range, I return the backing value of that range. If the age doesn't match, I return an empty string. That's what if(${age} >= 18 and ${age} < 24, 2, '') does.
    • I then concatenate all those if statements together. The effect is that only the value of the matching range is shown because the non-matching choices are empty strings. That's this part: concat(if(${age} < 18, 1, ''), if(${age} >= 18 and ${age} < 24, 2, '')...
    • I also add a greater than 45 age range for completeness.
  • Finally, I show the select, ${age_range_visible}, with the age ranges you wanted to confirm and compare the value of that to the value inside ${age_range}. If it's not the same, then I show an error and the enumerator cannot continue.

Here is the form if you want to try it out:
age-range.xlsx (10.9 KB)

Hi Yaw,

Thanks for the feedback,let me try it.

Hi Yaw,

Thank you so much.
The shared example has worked on my end.
Have a great weekend.

Regards,

Paul

1 Like