- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 09-20-2020 01:26 AM
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:
Existing Regular expressions
There are already a few Regular expressions available in the system:
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:
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:
The result
A valid email address format:
Invalid format:
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:
Common Regular expressions
For common Regular expressions, please refer to this article:
- 46,820 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
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
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
what if you're trying to allow new lines for example:
line1text
line2text
line3 text
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Regex validation can be used in catalog builder as well.