regex question

1130
4
03-27-2019 03:22 PM
neiden
by
New Contributor III

Hello! I feel like I'm really close with the answer but hitting a wall. I'm using a regular expression to constrain values for a web-only form. The pattern required is 2 uppercase letters followed by 6 digits. 

Here's what I'm trying:

regex(.,'^[A-Z]{1,2}\d{6}$') 

It almost works, except for it allows 7 numbers to be entered, and it needs to be only 6. 

Help? i'm sure it's a simple fix staring me in the face.

TIA! 🙂 

0 Kudos
4 Replies
Egge-Jan_Pollé
MVP Regular Contributor

I have had a quick look with Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript 

Your expression meets your requirements, according to the test:

for example GS654321 is highlighted, whereas DQ7654321 is not.

So there should be another reason your regex does not work...?

0 Kudos
neiden
by
New Contributor III

Thanks for checking the expression & for the website to do so. That will be

a great tool in the future.

So the interesting thing is that in the published web form, the expression

is working as intended, but in the form preview in Survey123 Connect, it

allows 7 digits to be entered. So perhaps a S123 Connect bug?

EarlMedina
Esri Regular Contributor

That would appear to be the case...

I don't know if it would matter, but you could re-write the expression. You originally had:

^[A-Z]{1,2}\d{6}$ 

  • To mean:
    • starts with capital letter, 1-2 repetitions (you stated 2, but I'll assume you meant 1-2)
    • ends in digit with 6 repetitions.

I suppose you can see what happens if you do:

^[A-Z]{1,2}[0-9]{6}$

You could also see if approaching this negatively produces the same problem but this would make the expression much longer.

-Earl

neiden
by
New Contributor III

I tried that - same result.

Then I realized something else - the regex constraint IS working as it should. However, the presentation is misleading. On closer examination I realized that entering 7 digits triggers the constraint message, so that's good. The bad thing is that the text in the field stays red once the regex condition is met (2 alpha's+6 digits), which misled me to believe there was an error. 

I'm sure users will be confused too, as the text stays red, even tho' the regex constraint is met. Users will think that what they've entered is incorrect, when in fact, it is valid.

Some more feedback....I wish that the constraint messages didn't appear in the web form, because it's super confusing to be presented with a message to enter valid values, even tho' nothing has been entered. I saw on other GeoNet postings that other users have encountered this, & that it will be addressed in a future release. 

Thanks for the help & quick replies! 

0 Kudos