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

You can do this with an onSubmit script in Portal - I was thinking of scripts that include GlideAjax calls that can't be used onSubmit in Service Portal, etc. to prevent form submission.

function onSubmit() {
    var primary = g_form.getValue('u_nationality');
    var secondary = g_form.getValue('u_second_nationality');
    var tertiary = g_form.getValue('u_third_nationality');

    if ((primary && primary === secondary) || (primary && primary === tertiary) || (secondary && secondary === tertiary)) {
        g_form.addErrorMessage('Citizenship fields must have unique values.');
        return false;
    }
}

If you wanted to be maybe a little more user friendly, you could also have onChange scripts on each of the three variables that does the same check of all three variables and alerts the user if any two are the same.  These scripts would not prevent submit - just let the user know before they try to submit, which may be useful if it is a form with lots of variables.