Willem
Giga Sage
Giga Sage

Field validation: Regular Expressions

Sometimes we want to help the user by validating if the input is correct/what we expect in the system. For example if the user inputs a phone number, url or email address.

Regular expression

Wikipedia: A regular expression (shortened as regex or regexp; also referred to as rational expression) is a sequence of characters that define a search pattern.

Basically we provide the system a pattern it can use to search any string value with and allow it to do some action when a match is found.

 


Using regular expression: Variable Validation Regex

You can use regular expressions throughout the system. If you want to validate a value input in the Service Catalog for a user requesting an item you can set up a Variable Validation Regex:

find_real_file.png

 

Existing Regular expressions

There are already a few Regular expressions available in the system:

find_real_file.png

 

 

Add a new one

You can add your own Regular expression to this. For example if we want to check if an email address that is entered is valid:

find_real_file.png

 

 

Apply it to a Variable

To apply it to a Question/Variable, go to the variable and select the regular expression you like to use in the Type Specification:

find_real_file.png

 

 

The result

A valid email address format:

find_real_file.png

 

Invalid format:

find_real_file.png

Reference the Docs article here: https://docs.servicenow.com/bundle/orlando-it-service-management/page/product/service-catalog-manage...

 

 


Using regular expression: onChange client script

We can get the same result using an onChange client script. With the added benefit that we can use an onChange script in a Catalog Client script, as well as on a form in the back-end.

 

To do this add the following code to an onChange client script on the field you want to validate:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var re = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/;
	if (!re.test(newValue)) {
        g_form.showFieldMsg('your_field name', 'Please enter a valid email address', 'error');
        return false;
    }
    return true;
}

 

Like so:

find_real_file.png

 


Common Regular expressions

For common Regular expressions, please refer to this article:

https://community.servicenow.com/community?id=community_article&sys_id=453fa36edbd7d0103daa1ea668961...

Comments
himanshu2310
Giga Expert

Hi Willem

 

First of all thanks for writing this article which is immensly helpful.

I have two questions:

 

1. When i use URL regex (http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*) in below code, i get compilation error.

 

var url = "https://www.google.com/test/test123";

var re = (http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)

gs.print(re.test(url));

 

Error:

 

Javascript compiler exception: syntax error (null.null.script; line 3)

 

2. I have this requirement to fetch a URL from a string. for example string "not able to access https://www.google.com/test/test123 " should return "https://www.google.com/test/test123".

 

I am a naive with regex so would appreciate if you could help.

 

Regards

Himanshu

 

Nathan Okh
Mega Sage

what if you're trying to allow new lines for example:

line1text

line2text

line3 text

Saptadeep
Tera Contributor

Regex validation can be used in catalog builder as well.

Saptadeep_0-1690832755741.png

 

Version history
Last update:
‎09-20-2020 01:26 AM
Updated by: