logo

NJP

Regexes for Catalog Items Variable Validation, part 2 [Madrid]

Import · Aug 10, 2019 · article

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

Hi there,

A while ago I wrote an article about the new V*ariable Regex Validation* feature which came with Madrid. A really nice feature which simplifies the validation of Variable values. For example, for checking if a variable would contain a valid IP address, you would generally setup a Catalog Client Script with some Regex. With the Variable Regex Validation, this has become a lot easier, no coding needed anymore, no Catalog Client Script to create anymore, no script no maintain and check after upgrades anymore, etc..

Read all about it in my previous article:

Service Portal Catalog Items: Regex Field Validation [Madrid]

To help you speed up using the Variable Regex Validation, I thought, let's provide the community with some example Question Regular Expressions. The examples below, are all-in use in our environment.

Out-of-the-box available

- Number- URL- Zip Code

We've added

- Currency ($)\$[0-9]*\.[0-9]{2}$- Currency (€)\€[0-9]*\.[0-9]{2}$- Email address\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$- IBAN[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}$- IP address\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b$- Letter (A-z)[A-Za-z]*$- Mac address[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}$- Zip Code (NL)

[1-9][0-9]{3}[\s]?[A-Za-z]{2}$

IBAN example:

image

How to add yourself

Below a screenshot of a blanc/new Question Regular Expression. You can get here thru navigating Service Catalog > Catalog Variables > Variable Validation Regex.

- Think of a name for your Variable Validation Regex, this will also be shown in the Variable Regex field on the Variable;- What validation message should be shown to the user? For example: Input is incorrect. If adding something new here, and you have a multi-language instance, also create a UI Message for the translation (sys_ui_message);

- The actual Regular Expression, like the ones listed earlier in this article.

image

---

And that's it actually. Not that much to it! Hope this helps some community members a bit. If you have some interesting Regex validations or thoughts about this or even if the regex could be improved, let me know!

Kind regards,

Mark Roethof

ServiceNow Technical Consultant @ Paphos Group---

LinkedIn

image

View original source

https://www.servicenow.com/community/developer-articles/regexes-for-catalog-items-variable-validation-part-2-madrid/ta-p/2325181