
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
04-21-2019 08:27 AM - edited 08-21-2024 10:08 AM
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!
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.
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.
---
And that's it actually. Hope you like it. If any questions or remarks, let me know!
C |
If this content helped you, I would appreciate it if you hit bookmark or mark it as helpful.
Interested in more Articles, Blogs, Videos, Podcasts, Share projects I shared/participated in? |
Kind regards,
Mark Roethof
ServiceNow Technical Consultant @ Paphos Group
---
- 10,287 Views

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi there,
Two common regexes I added in our environment:
Currency ($):
Email (you could also use the email type field of course!):
If my answer helped you in any way, please then mark it as helpful.
Kind regards,
Mark
---
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
This is good stuff, however I'm not seeing the same results.
I've used the out of the box Number validation regex on a single line text field. Entering a non-numeric character ($ in my example) triggers the onChange message beneath the variable field. However, when I click Submit the form hangs on submitting indefinitely without displaying an error message. I thought it might be a result of one of our customizations but I duplicated the issue immediately in a fresh personal developer instance.
Did you have to build the onSubmit validation script to verify that each field had been verified or am I missing something obvious?

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi there,
Could it be that this behavior occurs on a mandatory field? If so... a known bug 😞
https://community.servicenow.com/community?id=community_article&sys_id=012c02e3db7fb700190dfb2439961...
Kind regards,
Mark
---
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Although this is great, it looks like this overrides and stops any other onChange scripts you may have running against a variable.
E.g. I'm currently finding that if I am trying to multiply two integer variables together, and using the validation regex to ensure they are integers, my onchange scripts to multiply them are not firing at all when a user changes a value.
Strangely the onChange scripts do run when another script updates the variable value instead of a user input

- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Hi Thomas,
Haven't experienced this myself yet, so I have to reproduce. Though if you are certain about this odd behavior and it's reproducible, do log a case at HI for this!
Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP
---
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Validate if IP address is in correct format (xyz.xyz.xyz.xyz) else through a message and clear the field. using regex variable.How to Do that.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content