Regex function syntax

1508
2
Jump to solution
11-06-2018 01:20 AM
Schalkvan_Lill1
Esri Contributor

Hi all.

I'm having some syntax issues with the regex function added to the constraints field. If anyone can show me where I'm going wrong, that'll be great.

I have some special characters in a text field that I don't want to allow. If the user populates the text field with one or more of these characters, I want the validation to fail with an error message. The special characters I do NOT allow are: " or ` or > or < or ; or \.

I tried the following (and various iterations of it); 

regex(.,'([^<>";])')

but once I add the ' or the \, it fails to refresh the survey.

regex(.,'(^[a-zA-Z0-9,*!£%^&)(+=@:#~/?. ]+$)')

where I add all the characters I want to allow, but this fails to refresh when I add { or } or [ or ].

Any help appreciated.

Regards

Schalk

0 Kudos
1 Solution

Accepted Solutions
StephenM
Occasional Contributor II

The reason why it won't work with \ and ' is because \ is used to escape characters using regex, and ' is used to enclose the string.

I got this to work by escaping the \, then creating a separate string and substituting it into the expression.

I used a hidden field named "characters" and set the default to the string I want to use: ^[^<>";\\']+$

Then in the constraint field for the input: regex(.,${characters})

View solution in original post

0 Kudos
2 Replies
StephenM
Occasional Contributor II

The reason why it won't work with \ and ' is because \ is used to escape characters using regex, and ' is used to enclose the string.

I got this to work by escaping the \, then creating a separate string and substituting it into the expression.

I used a hidden field named "characters" and set the default to the string I want to use: ^[^<>";\\']+$

Then in the constraint field for the input: regex(.,${characters})

0 Kudos
Schalkvan_Lill1
Esri Contributor

Works a charm. Thanks very much Stephen! 

0 Kudos