Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Mandatory 10 digit phone number for certain fields

booher04
Tera Guru

I have a need for a client script for a catalog item that will not allow you to submit unless the phone number is 10 digits, however on the form I created there is a choice list that has 3 choices, change existing efax account, request new efax number and port existing number.  Each of these when selected shows different variables.  The script I used worked fine for other catalog items but this one has the choice list and if you have 1 selected, the fields for the other 2 are hidden and the script is still requiring it.  My question is, how would I add to the script I have here to say has to be 10 digits and change existing efax account is selected.  I put 3 client scripts for each choice as of now, is there a better way to achieve this?

function onSubmit() {

//Type appropriate comment here, and begin script below

var phone = g_form.getValue('current_number');

if(phone.length!== 10 && 'type_efax_request' == 'change_existing_efax_account'){

alert("Phone number should be 10 digits for current eFax number");

return false;

}

}

the choice field name is type_efax_request, then the choices for it are:

change_existing_efax_account

request_new_efax_number

port_existing_number

 

find_real_file.png

21 REPLIES 21

If we answered your question, would you mind marking it correct to close the thread. 

Thank you!!

It's not working for me for some reason.  I tried to use onChange however it threw me 3 errors on the form itself, and when I switched it to onSubmit it still isn't working.  It doesn't allow the script to submit.

function onSubmit() {

if(type_efax_request == 'change_existing_efax_account'){

var val = g_form.getValue('current_number'); //put your field name here
var patt = /\d{10}/;
if (val.length ==10)
{
var result = patt.test(val);
if (!result) {
g_form.setValue('current_number','');
alert('Please enter 10 digit phone number');
return;
}
}
else
{
g_form.setValue('current_number','');
alert('lease enter 10 digit phone number');
return;
}
}
}

There's a lot of ways to validate this;

Here's a way I've done it.

https://blog.jacebenson.com/validating-phone/

Which works great after you configure the top vars.

Looking at that script, which part of it will check the field to make sure it's one of the 3 from the drop down list, then the matching phone number field.  Each choice from the drop down has a different phone number field(as they are labeled differently).  

 

find_real_file.png

find_real_file.png

 

find_real_file.png

So you would have to have 3 scripts then.  One for each phone variable.