The CreatorCon Call for Content is officially open! Get started here.

Regex validation to allow specific special characters and disallow the rest of special characters

Aditya Banka2
Tera Guru

We have a regex validator on a catalog item variable where we are not allowing any of the special characters. So I was using [0-9a-zA-Z]{1,40}

 

AdityaBanka2_0-1683030115376.png

 

and then passing the value to another variable by converting it into lowercase using a catalog client script with below code.

 

name = name.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();

 

Now the ask is to allow specific special characters  “.”,"-" and "_" and disallow the rest of special characters.

 

Once I allow the specific special characters I need to convert it into lowercase and pass it into another variable.

 

Can someone please suggest me how to do it and test it using a background script.

2 ACCEPTED SOLUTIONS

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Aditya Banka2 ,
I trust you are doing great.
To allow the specific special characters "." , "-" , and "_" while disallowing the rest of the special characters, you can modify the regex pattern in the validator on the catalog item variable. You can use the following regex pattern to achieve that:

^[a-zA-Z0-9_.-]{1,40}$

To convert the value into lowercase and pass it to another variable, you can use a client script like this:

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue === '') {
    return;
  }
  var input = g_form.getValue('variable_name');
  var output = input.replace(/[^a-zA-Z0-9_.-]/g, '').toLowerCase();
  g_form.setValue('other_variable_name', output);
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

Aditya Banka2
Tera Guru

Hello Amit,

 

I have modified my regex as below which worked fine.

 

[0-9a-zA-Z-_.,]{1,40}

 

AdityaBanka2_1-1683048064402.png

 

and used below code in catalog client script

 

name = name.replace(/[^0-9a-zA-Z-_.,]/g, "").toLowerCase();

View solution in original post

5 REPLIES 5

Aditya Banka2
Tera Guru

Hello Amit,

 

I have modified my regex as below which worked fine.

 

[0-9a-zA-Z-_.,]{1,40}

 

AdityaBanka2_1-1683048064402.png

 

and used below code in catalog client script

 

name = name.replace(/[^0-9a-zA-Z-_.,]/g, "").toLowerCase();