Regex with condition

Hi everyone,

How can I build a regex with the following conditions:

If q1 = ‘1’, q2 can only accept range: 001 to 027
If q1 = ‘2’, q2 can only accept range: 028 to 065
If q1 = ‘3’, q2 can only accept range: 135 to 166

Can someone please help? Thank you.

regexCondition.xlsx (10.0 KB)

Do you need leading zeros? Otherwise you could just use numeric constraints.

Why use a text type instead of a cascading select_one, e.g. with search/fop-down appearance?


Q2 can be of numeric type. In any way I managed to build a constraint as follows:

selected(${q1}, ‘1’) and (${q2} >= 1 and ${q2} <= 27) or selected(${q1}, ‘2’) and (${q2} >= 28 and ${q2} <= 65) or selected(${q1}, ‘3’) and (${q2} >= 135 and ${q2} <= 166)

Thank you.

1 Like

But you always need brackets to separate and from or constructs.

And it might be done a bit shorter
(${q1} = ‘1’ and . >= 1 and .< 27) or (… ). or (…)
You only need selected(…) for select_multiple. The current question/variable value can be referenced with . (i.e. dot).

Hint: Use the online validator to check your form during development and before deploymrent.


Your code is more elegant and efficient.

1 Like

@wroos, :clap: :heart: :partying_face: