Using regex on a specific field using validateRule

Feel free to move this question to another section if you think it does not belong to the development section.

I need to run a validation on a field, which is should contain the following rule:

1234-12-123456

It should start with 4 numbers, then a dash, then 2 numbers, then dash and finally 6 numbers.

I went for the validation of program rule and got the following:

d2:validatePattern( <text>, <regex> )

But how to link it to a specific field, and what the regex that should I use ?

1 Like

Hi @im_officer_leb
Assuming this a tracker/even program and a program rule?

The text to validate would usually be inserted by creating a program rule variable that links to the data element you want to validate. Let us assume you create a program rule variable with the name myDataElement. Then the expression in your program rule would be for example:
d2:validatePattern( #{myDataElement}, '[0-9]{4}-[0-9]{2}-[0-9]{6}')

Markus

3 Likes

Thanks. That’s it.

1 Like