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

Error:

onChange script error: ReferenceError: type_of_toll_free_request is not defined function h_5bfa1650db8a1f009dc573d78c9619c2() { if(type_of_toll_free_request == 'change_ring_to_on_toll_free_number'){ var val = g_form.getValue('number_that_needs_changed'); //put your field name here var patt = /\d{10}/; if (val.length ==10) { var result = patt.test(val); if (!result) { g_form.setValue('number_that_needs_changed',''); alert('Please enter 10 digit phone number'); return true; } } else { g_form.setValue('number_that_needs_changed',''); alert('Please enter 10 digit phone number'); return false; } } }

 

Script: 

function onChange() {

if(type_of_toll_free_request == 'change_ring_to_on_toll_free_number'){

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

 

 

Are you using this code onChange of field "type of toll free request"? If yes, can you please change the code like the one given below.

if(type_of_toll_free_request == 'change_ring_to_on_toll_free_number'){ to

if(newValue == 'change_ring_to_on_toll_free_number'){

Let me know if the issue still persists.

 

Thanks.

I agree with Bhawana's comments

Before check the condition:

if(type_of_toll_free_request == 'change_ring_to_on_toll_free_number'){

get the value of type_of_toll_free_request field. It's looking undefined.

example:

var type_of_toll_free_request = g_form.getValue('type_of_toll_free_request') + '';

 

OR

var type_of_toll_free_request = newValue;

according to your requirement.

 

Hi Boohar,

I have modified your script below:

Please check now!!

-------------------------------------------------------------------

function onChange(control, oldValue, newValue, isLoading) {

    var type_of_toll_free_request = newValue;
    if(type_of_toll_free_request == 'change_ring_to_on_toll_free_number'){
        var val = g_form.getValue('number_that_needs_changed'); //put your field name here
        var patt = /\d{10}/;
        if (val.length == 10){
            var result = patt.test(val);
            if (!result){
                g_form.setValue('number_that_needs_changed','');
                alert('Please enter 10 digit phone number');
                return true;
            }
        }
        else{
            g_form.setValue('number_that_needs_changed','');
            alert('Please enter 10 digit phone number');
            return false;
        }
    }
    else{
        alert('Parent condition not mat');
        return false;
    }
}

---------------------------------------------------------------------------------

If looks helpful, please give your feedback accordingly.

 

Thanks,

Gopal

I actually tried both of your scripts.  neither are working.  The one Bhawana gave me did the same thing with the error as soon as the form loaded.  I changed it to onSubmit doesn't submit, just sits there.  I tried the other full script and it pops up the alert before the form loads, and then a different alert when you change the choice list.  After this, it lets you submit though.  I need it to pop up the message that it has to be 10 digits when submit is clicked.