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

Bhawana Upreti
Tera Guru

Hi,

In case of three script you can use on change client script for the field type_efax_request and use if..else statement.

and one of your if condition would be like the one given below to validate phone number along with type of efax:

if(type_efax_request == 'change_existing_efax_account'){

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

 

Thanks.

Thank you for the reply!  Couple of questions:
Do you need these:

var patt = /\d{10}/;
var result = patt.test(val);
if (!result) {
g_form.setValue('u_number','');

Could you just use:

if(type_efax_request == 'change_existing_efax_account'){

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


if(phone.length!== 10){


alert("Phone number should be 10 digits");


return false;

{

I ask because I am not setting the value of anything, rather just checking to make sure that
if the type_efax_request is change_existing_efax_account, then it has to be 10 digits. If it's
both of those throw the alert message.

The Regex says only match on 10 digits.  The checking the length, doesn't check character types.

Adding to Jacebenson, after checking that it is not any string, it will make field clear. To force user only enter digits. We are not setting any value.