Validate pattern for date range


I have a TEA called Day of birth which needs to be validated so that only two digits can be entered in this field, and that these two digits need to be between 01-31. I’ve inherited a PR which does the first part:

(!d2:validatePattern(A{REA- Day of birth},’^\d{2}$’)) && (A{REA- Day of birth} != ‘’)

PR Action: Show warning: Day of birth must be a two digit number for example 04 or 17

I can’t figure out how to add to this PR to validate the second requirement that the two digits need to be between 01-31. Any tips on how to achieve this would be much appreciated!



Hi @monika .

I have created a test program in Play 2.35.1 () which should be available until tonight if you want to test it. It is called PR Validation and works with what you are looking for. I didn’t invest time in changing the attribute names so your rules is applied on the attribute first name and not in the age.

Your rule should be as follows (I changed 01 to 04 so you see it fails on 03)

However, I don’t think this is very user friendly as you are forcing the users to put manually something like 03 instead of 3 which might confuse users. Also, you may want to use the attribute type “Age” which provides an input for this:




1 Like

Hi @jaime.bosque,

The PR you proposed works exactly as I hoped and I was able to replicate on our instance as well - thanks for this!

I take your point about the Age attribute. The issue with this is that we are literally only interested in Day of Birth, and want to discourage any users from entering the full date of birth, for data protection reasons. But that tip will come in handy for our other program :slight_smile:

Thanks again!


1 Like

Hi @jaime.bosque

Can you help me to see what is wrong in the following validation program rule? I am validating a tracked attribute telefone of phone number type. I want to it to start with the country code +258 following by 9 digits and show warning message to the user.

d2:hasValue(A{telefone}) && !d2:validatePattern(A{telefone},’^258[0-9]{9}$’)


I am afraid that you might be impacted by this bug: can you try removing the A{} and use directly the suggested options on the comments?

Hi @jaime.bosque,

The Program rule seems to be working. I think the problem was with quotes.

I was wondering if you could help me with another program rule that has been perplexing me. My problem is the same as the following post:

In a repeatable stage, I have a yes/no data element to track. That is, if the answer to this DE is No four consecutive times, then display a message and block program stage for future data entry. In other words, if the answer to the DE is No for 1st, 2nd, 3rd and 4th visits, display a message and block user input).

I did try this, but it is not working. Maybe using d2:countIfValue is not correct here.

V{program_stage_id} == ‘H1r8g2lxK4i’ && V{event_count} == 4 && d2:countIfValue( #{Patient arrived at HF}, false ) == 4

V{event_count} refers to number of visits

V{program_stage_id} == ‘H1r8g2lxK4i’ refers to the program stage Id of the repeatable stage

#{Patient arrived at HF} is the yes/no data element

Looking forward to a guidance

Hey @dhis2-android team

@ferdinandmussavene received @jaime.bosque support but still seems to be facing an issue!