How to make Checkbox selection mandatory

Snow Angel
Tera Expert

Hello,

Requirement is to have at least one or more selected before submitting form. How can I make this Catalog Item checkboxes mandatory.

"Required Provision" is a container so can't be made required.

"Please select:" is a label, and if I make the first option Mandatory then it conflicts with a UI Policy where if the last option is selected, every option above goes read-only. Same for the first option, if it is selected everything below gets read only. (pasted below) The right side of this container is reserved for other fields that pops depending on selection of one of these fields. Please help. Is this going to be a Catalog Client Script?

 

SnowAngel_1-1716404625402.png

SnowAngel_0-1716406317777.pngSnowAngel_1-1716406369761.png

 

I have tried below Catalog Client Script but even after selecting one checkbox, its not letting me submit the form. What's wrong with this approach?

function onSubmit() {
    var checkbox1 = g_form.getControl('opt_physical_access').checked;
    var checkbox2 = g_form.getControl('opt_it_remote_access').checked;
    var checkbox3 = g_form.getControl('opt_it_laptop').checked;
    var checkbox4 = g_form.getControl('opt_it_telephone').checked;
    var checkbox5 = g_form.getControl('opt_it_mobile').checked;
    var checkbox6 = g_form.getControl('no_physical_or_logical_access_required').checked;

    if (!checkbox1 && !checkbox2 && !checkbox3 && !checkbox4 && !checkbox5 && !checkbox6) {
        g_form.addErrorMessage("Please select at least 1 choice under Required Provisioning section");
        return false; // Prevent form submission if no checkboxes are selected
    }

    return true; // Allow form submission if at least one checkbox is selected
}


 

5 REPLIES 5

Community Alums
Not applicable

Hi @Snow Angel ,

I tried your script in my PDI and make some changes and it works for me please check below script 

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var checkbox1 = g_form.getValue('check_option_one');
    var checkbox2 = g_form.getValue('check_option_two');
    var checkbox3 = g_form.getValue('check_option_three');
    var checkbox4 = g_form.getValue('check_option_four');
    alert("1 = " + checkbox1 + " 2 = " + checkbox2 + " 3 = " + checkbox3 + " 4 = " + checkbox4)
    if (checkbox1 == 'false' && checkbox2 == 'false' && checkbox3 == 'false' && checkbox4 == 'false') { // getValue returns value in string so this is a reason I give values in string "false"
        alert("Here inside if");
        g_form.addErrorMessage("Please select at least 1 choice under Required Provisioning section");
        return false; // Prevent form submission if no checkboxes are selected
    }

    return true; // Allow form submission if at least one checkbox is selected
}

Please change variable name as per your requirments

Result 

When I didn't select any value 

SarthakKashyap_0-1716407178518.png

When I select one or more value 

SarthakKashyap_1-1716407232803.png

SarthakKashyap_2-1716407253700.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak

swathisarang98
Giga Sage
Giga Sage

Hi @Snow Angel ,

 

Did you try the option Selection required ? which is available OOTB no need for scripting you can make selection required for any of the checkbox in the sequence so if anyone one is selected it allow you to submit 

swathisarang98_0-1716408406054.png

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

Won't each checkbox would become required then?

 

@Snow Angel nope make any one of the 'checkbox' selection required it will make the label as mandatory so if any of checkbox(one or more) is selected it will allow you to submit

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang