November Happy Hour will be moved to Thursday December 5th.

How to use the "Regular Expression" feature on Text Areas

Vote:
 

Hello,

I am trying to use the Regular Expression feature on Text Areas and it is not working the way I am expecting. The only thing I can find about it in the documentation is to 'Click Change to enter a regular expression pattern (such as /\s*/).'

Specifically, I have a forms generator based on a pagetype and I want to add a character limit to Text Areas. First, I made a custom Text Area element using the `maxlength` attribute, however, I did not like that there wasn't any built in validation for that attribute. Second, I tried using the regular expression functionality with expressions such as `/^.{,10}$/`, `/\w{,10}$/`, or `/[\w\W]{10,}/`. I've used {10,} and {,10} because its not clear to me if I want my regex to pass or fail in order to trigger the validation. The only validation I get is that if I leave the field empty, it passes but if I enter any text at all, it fails. It does not matter how many characters I enter. 

Can anyone help me craft my regex in a way that will help me validate character limit?

#305741
Jul 26, 2023 22:41
LarryVictorio - Feb 07, 2024 23:56
Does the regex validator act as a whitelist or a blacklist?
I'm still confused by how to use it properly.
I want to NOT allow end users to enter URLs into a text area element.
Should I be looking to create a regex that defines what I allowing or what I want to exclude.
I can't seem to get it working either way.
Zach Graceffa - Feb 08, 2024 21:05
Larry, you are writing a Regex for what you are allowing. If the entered text is a match, no validation is triggered. If the entered text is NOT a match, then the validation is triggered.

That being said, you can flip that logic by using a NOT operator like [^a-z] (meaning no lowercase letters).

Also, make sure to not include '/' at the beginning and end of your regex as most of the documentation on this topic states. See my below comment for more detail on that.
Vote:
 

I was able to get the functionality I desired with the following regex: ^\w{1,10}$

It was the forward slashes (/) at the beginning and end of the regex that were the main problem. I did more searching and found out that this form validation documentation also had some information about the forms regex feature. It says that "The string a user enters must be a value you can pass into new RegExp() in standard JavaScript."

It is correct (from my testing) that you need to supply a value that will work with RegExp(). So, the documentation I linked to in my first post is incorrect, as forward slashes denote a regex literal. You do not pass regex literals into RegExp(), you pass regex strings. 

It would be more accurate for the documentation to say "Click Change to enter a regular expression pattern (such as \s*)."

#305795
Jul 27, 2023 18:05
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.