Validation for ID number 7th digit to be greater than 5

Hi

Can someone help in the validation on CommCare for checking whether the "7th digit" of a South African ID number is greater than 5 (to allow us to flag Male vs Female).

I want to restrict the ID number question to only allow males, hence, the 7th digit needs to be greater or equal to 5.

For example, the South African ID number is in the format of YYMMDDSSSSCAZ

ID Number 7th digit Gender

9901295674083 5 Male
9901290674083 0 Female
9901287674081 7 Male

I want to flag this on CommCare and not allow the Data capturer to proceed until the correct value is entered. I can easily do this on Excel, SQL or PowerBI but we want to prevent this from being captured on CommCare.

Also, in many cases we don't have full ID numbers being captured (i.e. the full 13 digits) due to many patients not coming though to clinics with their IDs and rather they present themselves with affidavits showing just their DOB. So in those cases, we are obliged to allow them to proceed and capture only the DOB as an ID number. Hence, the validation for the gender should only be applicable when the string-length is greater than 6.

Hope someone is able to assist. I have tried a few expressions but none has worked.

I would suggest using a regular expression. There are a few examples online which you could potentially use (you should test them first):

In order to validate the gender digit you would need to use two different expressions and an if statement:

if(/data/gender = 'M', regex(/data/id_number, /data/id_regex_m), regex(/data/id_number, /data/id_regex_f)

(I've used hidden values to store the regex expressions as a way to make it easier to see what this expression is doing since the regexes are quite long).

You could wrap the whole expression in another if statement to check the length:

if(string-length(/data/id_number) < 6, true(), <expression from above> )