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