Digit validation

Hi everyone,

National phone numbers in my country consist of 9 digits. I would like to validate attribute phone number in a tracker program so that it contains only +253 country code + 9 digits ,e.g., +268 86 800 9330 and display a message for the user if the validation is violated, i.e., the user cannot save the personal details if there’s a response that violates the validation.

Could you help me an expression?

Thanks

1 Like

Hi @fernandoshake,

It’s not clear what restriction you have for the country code and whether the user will have to type + and whether you’d allow them to type 00 instead. Additionally, since you mentioned two variations (253) and (268), I only made the restriction to the first digit in the country code to be the digit 2.

An example of an expression you could use is d2:validatePattern(A{phoneNumber},'\\+2\\d{11}') this is tested and working properly but can be customized more as for now it only requires that the user types the plus sign then the digit 2 and then 11 digits.

For the action, since you don’t want the use to proceed without the correct phone number then the action should be to “Show Error”

Hope this helps! Please feel free to post back to the community if you need to customize it more. If it solves the issue please mark the post as solved.

Thanks!

1 Like

Hi @Gassim,

We would like the users to only enter 9 digits. We would be happy if the country code should be included in the validation pattern, so that users do not have to enter +258.

Thanks

1 Like

Hi @fernandoshake,

It’s possible to add the country code using a program rule after the users have entered the phone number so the validation here would be that the user entered only 9-digits:
d2:validatePattern(A{phoneNumber},'\\d{9}')

Additionally, if all phone numbers start with a certain number you could add that to the validation. For example, where I live, all mobile phone numbers start with a ‘7’ whereas home numbers start with ‘01’ - ‘07’.

1 Like

Hi @Gassim,

We want to validate a code so that it starts either with GA or MP following by 6 digits. e.g GA235742 or MP378053. In relation to the following expression, how can I change it to get the result I want?

Thanks

Hi @fernando

The regex expression to use would be, ^(GA|MP)\d{6}$

So the rule expression would be: d2:validatePattern(A{attribute},'^(GA|MP)\d{6}$')

1 Like