logo

NJP

Service Portal Catalog Items: Regex Field Validation [Madrid]

Import · Apr 21, 2019 · article

Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

Hi there,

A short article to share gained knowledge about applying field validation on variables on Catalog Items on the Service Portal.

Pre-Madrid

Pre-Madrid, we could script field validations through Catalog Client Scripts. Using regex or some other methods. You would need to script, add a message, and take in consideration that doing this onChange did not prevent the form from submitting.

An example onChange Catalog Client Script to check if a Single Line Text input would consist of exactly 8 integers:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {
        return;
    }

    if((isNaN(newValue) == true) || (newValue.length != '8')) {
        g_form.addErrorMessage(getMessage('invalid_number'));
    }

}

Madrid

With Madrid, a cool choice field on the Variable form has been added!

image

The choices are stored within the Currency Regular Expression table [question_regex]. Out-of-the-box validations available are:- Number- URL

- US Zip Code

Adding a new validation is pretty easy. See the example below for a dollar currency.

image

Once having the Validation Regex applied to the Variable, the validation is active onChange of the field. If the field is incorrectly filled, an error message is shown below the variable.

Good to know: you don't have to add extras to prevent the form from submitting with an incorrectly formatted variable. If hitting the submit button, a clear addErrorMessage appears at the top of the screen.

image

---

And that's it actually. Hope you like it. If any questions or remarks, let me know!

Kind regards,

Mark Roethof

ServiceNow Technical Consultant @ Paphos Group---

LinkedIn

image

View original source

https://www.servicenow.com/community/developer-articles/service-portal-catalog-items-regex-field-validation-madrid/ta-p/2325331