How do you limit a variable to 10digits?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2022 12:06 PM
Hi, The requirement is> minimum of 10 digits is required for a variable.
Is this the correct script?
Here is the script. I created a on change script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var val = g_form.getValue('Facility_npi');
var pattern = /\d{10}/;
if (pattern.test(val))
alert('looks like 10 digits');
else
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2022 10:52 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2022 10:59 AM
Because if you check for
if (lengthis > 9)
it says greater than 9
If you want 10 minimum then you need to change 9 to 10 in above if condition

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2022 09:14 PM
Hi Navneet
Regular expression to check is as follows.
/^\d{10,}$/

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2022 09:17 PM
Hi,
Beside using Client Script, it's also possible to use Variable Validation Regex (Service Catalog > Catalog Variable > Variable Validation Regex).
The above regular expression will also check if the value is a digit.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2022 09:27 PM
BTW, is the name of the field actually "Facility_npi" with uppercase "F"? ServiceNow default to all lowercase. I've changed to lowercase in the sample script below.
Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var val = g_form.getValue('facility_npi');
var pattern = /^\d{10,}$/;
if (pattern.test(val)) {
g_form.showFieldMsg('facility_npi','looks like 10 digits', 'info');
} else {
g_form.showFieldMsg('facility_npi','require at least 10 digits', 'error');
}
}
Execution result
case 1: less than 10 digits.
case 2: 10 digits