Regex validation for participant id to have numbers and hypens only

I need to validate that users to input a valid participant ID with 3 dashes, but the ID does not have a fixed length of integers. e.g these are valid IDs.

  1. 154-12-9-1
  2. 1-1-7-12
  3. 12-100-5-1
  4. 14-10-3-2017
    The first set is the Village ID, secondly is the Compound ID, third set is Household ID, 4th is the Member ID.
    Challenge 1: I don’t have control on the ID length, but it has to have four sets of integers separated by hypen (-)
    Rules:
    1.Village code a a maximum value of 999(Length of 1-3 digits)
    2.Compound code maximum value of 999 (Length of 1-3 digits)
    3.Households code maximum value is 9 ( Households cannot exceed 9 in a compound (Length of 1 digit))
  5. Member code can have maximum of 9999(Length of 1-4 digits)
    Anyone with idea(S)?

Hi Martin!

CommCare supports validating inputs using regular expressions with the
regex()
https://confluence.dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-regex
function,
which is a great fit for the problem you’ve described.

It’s often helpful to test potential expressions with an online tool
https://regex101.com/ before configuring them in CommCare, I think the
easiest first one I’d try would be something like

[0-9]±[0-9]±[0-9]±[0-9]+

which would validate that an input contained 4 sets of numbers of arbitrary
length separated by dashes.

-Clayton