Check box validation

Jenjoe12
Tera Contributor

Hi All,

I have a requirement on checkbox validation ,seeking your help how to achieve using onsubmit client script

There are four checkboxes under a label

1) Make first checkbox always mandatory selection

2)Remaining 3 checkboxes - select at least one check box,

3) Message to show if only mandatory check box is selected , you have to select at least one in remaining check boxes

4)Message to show vice versa when at least one of the 3 check boxes are selected, select the first option which is mandatory.

 

I tried to create a label and group all the 4 checkboxes below the label. But that is working as when the first checkbox is checked , then it removes the other 3 checkboxes from being mandatory.

Please provide your inputs.

1 REPLY 1

Sid_Takali
Kilo Patron
Kilo Patron

Hi @Jenjoe12 Try below code

function onSubmit() {
  
    var firstCheckbox = g_form.getValue('first_checkbox_id');  // replace your checkbox name
    var secondCheckbox = g_form.getValue('second_checkbox_id'); 
    var thirdCheckbox = g_form.getValue('third_checkbox_id'); 
    var fourthCheckbox = g_form.getValue('fourth_checkbox_id'); 

    var isFirstChecked = g_form.getValue('first_checkbox_id') == 'true';
    
    var isAnyOtherChecked = g_form.getValue('second_checkbox_id') == 'true' ||
                            g_form.getValue('third_checkbox_id') == 'true' ||
                            g_form.getValue('fourth_checkbox_id') == 'true';

  
    if (!isFirstChecked) {
        if (isAnyOtherChecked) {
            // Show message if any of the other checkboxes are selected but the first one is not
            g_form.addErrorMessage('You must select the first checkbox, which is mandatory.');
            return false; 
        }
    } else {
        if (!isAnyOtherChecked) {
            g_form.addErrorMessage('You need to select at least one of the remaining checkboxes.');
            return false; 
        }
    }

    return true; 
}