How do you limit a variable to 10digits?

Navneet3
Tera Expert

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

find_real_file.png

 

 

find_real_file.png

 

 

 

15 REPLIES 15

Jaspal, I enetered 10 digits but I am still getting the following.

 

find_real_file.png

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

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Navneet

Regular expression to check is as follows.

/^\d{10,}$/

 

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.

https://docs.servicenow.com/bundle/paris-servicenow-platform/page/product/service-catalog-management...

 

Hitoshi Ozawa
Giga Sage
Giga Sage

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.

find_real_file.png

case 2: 10 digits

find_real_file.png