Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Mark Roethof
Tera Patron
Tera Patron

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 thru 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!

find_real_file.png

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.

find_real_file.png

Onces 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 extra's 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.

find_real_file.png

---

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

👍
If this post helped you in any way, I would appreciate it if you hit bookmark or mark it as helpful.

Interested in more articles, blogs, videos, and Share Projects on Service Catalog I published?
Service Catalog


Kind regards,
Mark

---

LinkedIn

Comments
Mark Roethof
Tera Patron
Tera Patron

Hi there,

Two common regexes I added in our environment:

Currency ($):

find_real_file.png

Email (you could also use the email type field of course!):

find_real_file.png

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

LikeARabbit
Giga Expert

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 Roethof
Tera Patron
Tera Patron

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

---

LinkedIn

Thomas Wright-1
Mega Expert

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 Roethof
Tera Patron
Tera Patron

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

---

LinkedIn
Community article list

Color
Kilo Contributor

 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.

ankit_sharma487
Kilo Guru

@Mark Roethof , thanks for the helpful article

Version history
Last update:
‎04-21-2019 08:27 AM
Updated by: