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

nayanawadhiya1
Kilo Sage

Hello Aditya,

 

You can use like that -

 

[ A-Za-z0-9_@./#&+-]

 

 Please mark it correct if it fulfills your requirement.

Muhammad Khan
Mega Sage

As per your existing regex, you can utilize below regex.

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

Jan Prochazka
Tera Guru

Hi @Aditya Banka2 ,

 

Firstly, let me answer directly your question:

You can validate a "Regular Expression" variable entered by your users using another Regular Expression checking whether the RegEx entered includes only allowed special characters. This check can be implemented as a catalog client script (onChange). Upon a change of the Regular Expression variableit will be validated by this catalog client script and if it doesn't comply with your rules then the message will be provided to user prompting to fix it up (telling why the entry was not accepted). More about catalog client scripting here:

https://docs.servicenow.com/bundle/tokyo-application-development/page/script/client-scripts/concept/... 

 

I also recommend maintain such validating RegEx as a system property to make it configurable.

 

Secondly, let me provide some additional ideas:

What's the exact business case for such catalog item? What business value it should bring to users? I'm asking, because there are very often more straightforward, more clear and even cheaper solutions for many custom business desires. What I'm trying to do is to encourage you in searching the best fitting solutions delivering the value in as close as possible to out-of-the-box, leveraging maximally ServiceNow capabilities.

 

If the value your catalog item is supposed to provide to your users is only the regular expression validation, why not using a dedicated, free available a really fine tuned tools for that, like: https://regex101.com/

 

You can even direct your users to this we page together with providing them info how to use it via Content Item.

Content Items are, in ServiceNow, a kind of service catalog items that provide information instead of services. One type of Content Item is External Content. This is intended to be used exactly for cases like this. It's a controlled way of providing your users with shortcuts to valuable external resources. You can control who can access it and where it is placed withing your service catalog.

 

You can find more details about Content Items here:

https://docs.servicenow.com/en-US/bundle/utah-servicenow-platform/page/product/service-catalog-manag...

 

I hope this helps, if yes, please mark it as helpful.

 

Regards,

Jan

 

 

 

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