D2 function

Dear all,
Does the use of d2 function require any installation ? It isn’t regognized by Dhis2 when I try to add the program validation rule. (d2:length(A{}))
Fyi I use DHIS2 2.25


Hi @pa_trick67
Had you gone through the DHIS2 User Manual: 2.25 Table 27.11. Functions to see how the expression format?


Yes! I had gone their and it worked in just one expression but I want to test many conditions like :

d2:left(A{},1) != “M” && d2:left(A{},1) != “F” || d2:right(A{} }, 2, 3) > 31 && d2:right(A{} }, 4, 5) > 12 && d2:right(A{} }, 6, 7) > 99 || d2:length(A{}) != 12

the A{} is my attribute kind of : “M140298RAHPA” composed by :

  1. Gender : M or F
  2. date of birth: ddmmyy
  3. 3 characters of Last Name
  4. 2 characters of first Name

What I want is to show warning if the the Attribute is malformed.
I noticed whenever I change the program rule I have to clear the browser cache otherwise it does take into account my last changes.

Your help is really appriciated.
Thanks a lot



Hi there @pa_trick67 !
You are correct, when changing the program rules you have to either delete the cache, OR manually increase the version of the program in the edit program screen.

In cases where you want to check/validate a complex pattern like above, I would all in all suggest using another function that allows regular expression matching. This would if you want simplify the expression into one regular expression for checking the whole expression in one operation:

d2:validatePattern(A{}, ‘[MF]{1}[0-3][0-9]|0-1][0-9][0-9]{2}[a-zA-Z]{5}’)

(regular expression written as example, not tested and syntax checked)

There seems to be some minor syntax errors in the expression examples above, if you end up using the d2:right, d2:left and d2:substring, the errors should be fixed:

d2:right(A{} }, 2, 3) > 31

You probably meant to use “d2:substring” and also remove the extra curly brace after the A{}.


Hi @Markus

I have tested this but still no work :
d2:validatePattern(A{} , ‘[MF]{1}[0-3][0-9][0-1][0-9][0-9]{2}[a-zA-Z]{5}’)
that I have tested here : https://regex101.com/

The deal is that in the regex above I can not check if date is valide.
ex: 340189 or 121990
Is there a function like d2:IsDate() ?.

Finally, I just control the first character and the length :

d2:left(A{}, 1) != “M” && d2:left(A{}, 1) != “F” || d2:length(A{} ) != 12

it works in DHIS2.

I have manually increased the version of the program 9 to be 10.
We planned to update in DHIS2 2.30 I hope I can do that condition.

Thanks you my friend.



Hey @pa_trick67!
There is no d2:isDate() currently. It would be great if you can describe the use case by creating a user story in jira.dhis2.org.
You can use a regular expression that is more complex for a stricter date validation. Here is a stackoverflow that shows some different options(I would go for the top one :slight_smile: )

d2:condition is not supported as a function in program rules, but usually there is other ways of achieving the same functionality. What is the use case for the d2:condition?

Best regards,


Hi @pa_trick67,

Did you manage to get your functions working or you updated a JIRA ticket for the same?



How do I subtract [EndDate - StartDate] using d2 functions?

There are d2:daysBetween, monthBetween like functions. Try them as per your need.

I tried, getting errors. I wanted an example. Thank you.