Regex constraint to validate alphanumeric

#1

I am trying to use RegEx to validate alphanumeric value.
Constraint = “regex(.,('^[A-Z0-9]0[1-9]demogForm.xlsx (17.6 KB)
{3}$'))”

#2

Can you describe or post some examples of what strings you are trying to check. Your regex looks like a valid regex expression, but without some test cases it’s not possible to determine if it’s fit for purpose. Thnx

#3

Thanks,
I would like to check alphanumeric character ranges.
Example.
a) 001-999 Or.
b) A01-A99

#4

regex(., '^[0-9]{3}$')

regex(., '^[A-Z][0-9]{2}$')

(assuming you want to allow prefix letters other than 'A') :slight_smile:

I highly recommend hopping on somewhere like regex101.com and playing around. It’s simple to try out different regex expressions against data you type in, and finding out in real-time what does/not work is a great way to learn the seemingly indecipherable language of regular expressions.

#5

Thanks Xiphware for your help.

#6

Hi,
I wanted to validate number with combination of alphabet with regex(., '^[A-Z][0-9]{2}$'). But throws error while converting through application designer.
“Error: Error: Unexpected token . interpretting formula: regex(., '^[A-Z][0-9]{2}$') on sheet: survey row: 17 column: constraint”

Then after I changed regex to regex('.', '^[A-Z][0-9]{2}$') it converted successfully.
But when I tried to preview the survey screen it throws error: Exception while evaluating constraint() expression. See console log.
The console log shows:
E/Exception: 'regex is not defined' in user-defined expression: regex('.', '^[A-Z][0-9]{2}$') on survey row 17 column: constraint

#7

Sorry, my response was wrt ODK1. But I now assume you are in fact using ODK2?

For ODK2, see Problem in Implementation of Regular Expression: ODK2.0 for a way to perform regex tests.

1 Like
#8

Thanks Xiphware for this one.

1 Like