Regex Validation

(Jumai Muhammed) #1

Hello Everyone,

I am trying to use some validations on questions in a survey to reduce the percentage of dirty data that comes in, I have tried to apply the the regex syntax below for the entry of a unique id in the format KN-GGG-DE-012

regex(., ‘(?:|KN|KD|KT|JG|PL)-[A-Z]{3}-[A-Z]{2}[0-9]{3}$’)

KN,KD,KT,JG,PL are the assigned unique values based on location but after applying the validation doesn’t work.

0 Likes

(Ethan Soergel) #2

It looks like your expression is missing the last dash, the one between DE and 012.

When testing regexs like this, I’d highly recommend building the expression in an online regular expression tester, as it’s much quicker to test in isolation. You can also build up the expression piece by piece, testing along the way, so when there’s a failure, you know where it comes from.

0 Likes

(Jumai Muhammed) #3

Hi Ethan,

Thanks a lot, yes i built this piece by piece using the regexr builder but i had to do some manipulations because the expression as is doesn’t pull any matches in the regexr when i type in a unique id. The code below is what actually worked in the expression builder but doesn’t work in Commcare

((.,’|KN|KD|KT|JG|PL)-[A-Z]{3}-[A-Z]{2}-[0-9]{3})

0 Likes

(Ethan Soergel) #4

Hi Jay,

I just tried out the following in CommCare, and can confirm that it evaluates to True:

regex(‘KN-GGG-DE-012’, ‘(?:|KN|KD|KT|JG|PL)-[A-Z]{3}-[A-Z]{2}-[0-9]{3}$’)

One thing I noticed was that your original expression included this type of apostrophe: ‘ rather than the basic ’

The former gives me an error in CommCare. This can sometimes happen if you write the expression in a rich text editor such as Microsoft Word. It may also have been the forum software transforming it in your message, I’m not certain. Can you try pasting this expression exactly and confirming that the apostrophes are simple?

regex(., ‘(?:|KN|KD|KT|JG|PL)-[A-Z]{3}-[A-Z]{2}-[0-9]{3}$’)

0 Likes

(Jumai Muhammed) #5

Hi Ethan,

Thanks a lot, this is what happens when i use the expression exactly how you have posted it here

The expression below seems to be working now:

regex(., ‘(?:|KN|KD|KT|JG|PL)-[A-Z]{3}-[A-Z]{2}-[0-9]{3}$’)

0 Likes

(Simon Kelly) #6

Hi Jay, I think the issue with the one Ethan pasted is that it contained ‘fancy’ unicode quotes instead of the normal ASCII quotes vs ' (notice how the unicode ones are are slanted). This is often a challenge when copying things from a web browser or document.

Glad you got it working though.

0 Likes

(Jumai Muhammed) #7

Hi Simon,

Thanks a lot. About to open a new topic, would be grateful for the assistance.

0 Likes