- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2023 05:46 AM
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}
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2023 08:11 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2023 10:23 AM
Hello Amit,
I have modified my regex as below which worked fine.
[0-9a-zA-Z-_.,]{1,40}
and used below code in catalog client script
name = name.replace(/[^0-9a-zA-Z-_.,]/g, "").toLowerCase();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2023 10:23 AM
Hello Amit,
I have modified my regex as below which worked fine.
[0-9a-zA-Z-_.,]{1,40}
and used below code in catalog client script
name = name.replace(/[^0-9a-zA-Z-_.,]/g, "").toLowerCase();