Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Check box restrictions based on combination

Vijay Baokar
Kilo Sage

Hi Folk,

 

I have requirement and still i didn't find the correct logic to get this implemented.

Scenario: I have 5 check boxes as below:

1. Laptop

2. Mobile

3. Desktop

4. Printer

5. Mouse

 

currently user can select all check boxes at a time but i want to let user select only check boxes as per below combination,

 

1. Mobile

2. Mobile & laptop

3. Mobile & desktop

4. Mouse

5. Mouse &Mobile

6. Printer

7. Printer , Mobile & Desktop

 

user should not be able to select all combination of check boxes.

 

Thanks in advance for any help.

1 REPLY 1

Robbie
Kilo Patron
Kilo Patron

Hi @Vijay Baokar,

 

Please see a reply I made to a post just a few days ago solving to a pretty much similar requirement. The only difference would be restrict this to 1 rather than 3.

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 = 1; //You can chqange this value to whatever required
    var maxCount = 1; //You can chqange this value to whatever required

    var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
    if (!passed) {
        //Abort the submit
        alert('Please select ' + '1.'); //You can chqange this value to whatever required
        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);
    }
}