- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2023 06:33 AM
I have a string field (tracking_numbers)
In this string field we are allowing our technicians to put in 12 digit tracking numbers separated by a comma and space:
Example: 123456789321, 444555666777, 877755447733
3 tracking numbers.
How do I create a RegExp that will validate that each of these tracking numbers are:
- only numbers
- each are 12 digits long?
I created an onChange client script, but it doesn't seem to be working. Any help would be most appreciated. I also noticed I keep getting an unterminated string constant error. Not sure what to do here.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var regexp = /^[0-9]{12}*(,)?[0-9]{12}*$/; // Check that they are 12 digits and numeric
var str = newValue.trim(); // trim leading and ending spaces
if (!regexp.test(str)) {
g_form.clearValue('tracking_numbers');
g_form.showFieldMsg('tracking_numbers', 'Tracking numbers should be numeric entry only and be 12 digits each', 'error');
} else if (str != newValue) { // if there was leading or ending spaces, set the value to trimmed value
g_form.setValue('tracking_numbers', str);
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2023 06:39 AM - edited 03-29-2023 06:41 AM
Hi,
You can try using this as your regexp:
/^[0-9]{12}(,[0-9]{12})*$/
I'm unsure if your trim is causing any issues and ServiceNow should possibly remove those spaces, so you may not need that? Try without first, etc.
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2023 07:28 AM
Hi Ondrej,
I am a bit confused as I simply copied and pasted his regexp. Is there something that I am missing that is incorrect?