Script to Prevent User from Selecting More Than Three Checkboxes

dmullinax
Mega Guru

We want to prevent the user from clicking more than a specific number of checkboxes.  Is there code for this in a Catalog Client Script or UI Policy?  What is the best practice?

checkboxes.jpg

1 ACCEPTED SOLUTION

Robbie
Kilo Patron
Kilo Patron

Hi @dmullinax,

 

Kudos to @Hitoshi Ozawa who has provided a solution to this exact question previously.

Check the below link:

https://www.servicenow.com/community/developer-forum/how-to-make-3-checkboxes-at-most-and-least-mand...

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

 

For future usage and in case of broken links, here's the script courtesy of: @Hitoshi Ozawa

function onSubmit() {
    //Set the mandatory checkbox variable names and total mandatory count here
    var mandatoryVars = 'checkbox1,checkbox2,checkbox3,checkbox4,checkbox5,checkbox6,checkbox7,checkbox8,checkbox9,checkbox10,checkbox11,checkbox12,checkbox13,checkbox14,checkbox15,checkbox16';
    var mandatoryCount = 3;
    var maxCount = 3;

    var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
    if (!passed) {
        //Abort the submit
        alert('Please select ' + '3.');
        return false;
    }

    function forceMandatoryCheckboxes(mandatory, count) {
        mandatory = mandatory.split(',');
        var cnt = 0;
        for (var i = 0; i < mandatory.length; i++) {
            if (g_form.getValue(mandatory[i]) == 'true') {
                cnt++;
                if (cnt > count) {
                    return false;
                }
            }
        }
        return (cnt == count);
    }
}

 

View solution in original post

4 REPLIES 4

Robbie
Kilo Patron
Kilo Patron

Hi @dmullinax,

 

Kudos to @Hitoshi Ozawa who has provided a solution to this exact question previously.

Check the below link:

https://www.servicenow.com/community/developer-forum/how-to-make-3-checkboxes-at-most-and-least-mand...

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

 

For future usage and in case of broken links, here's the script courtesy of: @Hitoshi Ozawa

function onSubmit() {
    //Set the mandatory checkbox variable names and total mandatory count here
    var mandatoryVars = 'checkbox1,checkbox2,checkbox3,checkbox4,checkbox5,checkbox6,checkbox7,checkbox8,checkbox9,checkbox10,checkbox11,checkbox12,checkbox13,checkbox14,checkbox15,checkbox16';
    var mandatoryCount = 3;
    var maxCount = 3;

    var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
    if (!passed) {
        //Abort the submit
        alert('Please select ' + '3.');
        return false;
    }

    function forceMandatoryCheckboxes(mandatory, count) {
        mandatory = mandatory.split(',');
        var cnt = 0;
        for (var i = 0; i < mandatory.length; i++) {
            if (g_form.getValue(mandatory[i]) == 'true') {
                cnt++;
                if (cnt > count) {
                    return false;
                }
            }
        }
        return (cnt == count);
    }
}

 

dmullinax
Mega Guru

Thank you so much.  Is there any way to make it even more interactive?  That is, in an onchange event?

dmullinax
Mega Guru

I got it working with an onchange event.  It would be great if there was a way I could centralize this code.  Because for now I have to copy and paste this script into each onchange event that corresponds to the appropriate checkbox, but for now you got me going.  Thanks!