Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Validation regex not working

Not applicable

Hi all,

Im trying to create a validation regex for a string field

regex is : 

^([a-zA-Z\d]{2}[-.][a-zA-Z\d]{4}[-.][a-zA-Z\d]{4}[-.][a-zA-Z\d]{3}|[a-zA-Z\d]{6}[-.][a-zA-Z\d]{3}[-.][a-zA-Z\d]{4}[-.][a-zA-Z\d]{1})$
 
The requirement is it should allow both formats below, and can contain numbers and letters(case insensitive) and delimters can be - or .
xx-xxxx-xxxx-xxx
xxxxxx-xxx-xxxx-x
 
when I run the above regex in background script, it works fine. But not in catalog item in portal
Can someone suggest me a solution, please
 
Thanks
Bimsari
5 REPLIES 5

Mark Roethof
Tera Patron

Hi there,

 

Since you are mentioning in background script it works fine, though in catalog not: please share how you are trying this exactly and what is failing.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Not applicable

Hi Mark, below is the background script, all the test values are working

// Define the regex pattern
var regexPattern = /^([A-Za-z\d]{2}-[A-Za-z\d]{4}-[A-Za-z\d]{4}-[A-Za-z\d]{3}|[A-Za-z\d]{2}\.[A-Za-z\d]{4}\.[A-Za-z\d]{4}\.[A-Za-z\d]{3}|[A-Za-z\d]{6}-[A-Za-z\d]{3}-[A-Za-z\d]{4}-[A-Za-z\d]{1}|[A-Za-z\d]{6}\.[A-Za-z\d]{3}\.[A-Za-z\d]{4}\.[A-Za-z\d]{1})$/;

// Sample value to test
var valueToTest = "0182WA.203.6010.5";
var val2 = "0182WA.203.601q.5";

// Check if the value matches the regex
if (regexPattern.test(val2)) {
    gs.log("The value matches the pattern.");
} else {
    gs.log("The value does not match the pattern.");
}
 
But when added in validation regex for catalog item variable it shows as error:
BimsariLekamge_0-1723436197643.png

 

 

Don't see it yet, though just created the same Regex Validation and tested it with a Catalog Item, instantly works fine 😅

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Not applicable

BimsariLekamge_1-1723436265829.png

this is the portal error