Vulnerability in EPiServer.Forms

Try our conversational search powered by Generative AI!

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



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?

Jul 26, 2023 22:41

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*)."

Jul 27, 2023 18:05
* 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.