Prevent submission of a form with an onChange() client script

Gagana B N1
Tera Contributor

Hi,

 

There are three fields on the form New Primary Citizenship, New Secondary Citizenship, and New Tertiary Citizenship fields- which should prevent the user from submit the forms with same field values. However, the script is working but form submission is not working....

 
 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    var primary = g_form.getValue('u_nationality');
    var secondary = g_form.getValue('u_second_nationality');
    var tertiary = g_form.getValue('u_third_nationality');
 
    g_form.clearMessages();
 
    if ((primary && primary === secondary) || (primary && primary === tertiary) || (secondary && secondary === tertiary)) {
        g_form.addErrorMessage('Citizenship fields must have unique values.');
        g_form.setSubmit(false);
       
    } else {
          g_form.setSubmit(true);
    }
}
5 REPLIES 5

Anurag Tripathi
Mega Patron
Mega Patron

Can you elaborate what is the expected behavior?

When does this script trigger(on change of which field)

 

On Change script runs on change of a field, has nothing to do with submit. Are you trying to force submit the form in this script?

-Anurag

I have written same script thrice for three fields 'u_nationality', 'u_second_nationality', 'u_third_nationality'. Error message should popup when the user select same value to the next field and if same value selects it should prevent submitting the form

Brad Bowman
Kilo Patron
Kilo Patron

If you are not using Service Portal / ESC / ... change this to an onSubmit script and use

return false;

to prevent submit, while doing nothing if the error conditions are not met so that the form should submit.  If you are using Service Portal, stick with the onChange script, but clear a mandatory variable value when the error message is displayed so that the form cannot then be submitted without repopulating it and re-triggering the script.  Again with this script, you would do nothing if the conditions which display the error message are not met

I am using this for Service Portal, Is there any other way without clearing a mandatory variable value when the error message is displayed to prevent form submission?