Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Multi-select question but limit respondents to checking 3 of the items listed

I have a Discover Sawtooth account, which only allows unlimited checks in a multi-select question.  I would like to include a multi-select question of a list of 12 items but only allow respondents to select 3 of the 12 items.

Can anyone suggest code to add to the code editor text box that would allow me to restrict multi-select responses to 3? Thanks!
asked Apr 2, 2021 by NewSawtoothUser2021 (120 points)

1 Answer

0 votes
Hi,  I'm sorry this functionality is not yet built into Discover.  It will be coming eventually.  For now you can include the code below.  This will overwrite our existing JavaScript function and allow you to modify it to your needs.

Go to your Select question and click on the "</>" button above the question text.  Then paste in the code below.  Notice that I've changed juste the top line to have "intMax = 3".  Sorry for all of the code.  It really is just a copy paste of the original with that small tweak at the top.


<script>
    function SSI_CheckBoxCheck(strVarName, strQName, strErrTxt, intQNum, ListArray, intMin, intMax, NoneItemArray) {
        
        //Change this value to the max.  
        intMax = 3;

        //The line below can be uncommented and changed if you want to change the minimum number of checks.
        //intMin = 1;

        var blnValid = true;
        var i = 0;
        var intNumSelected = 0;
        var strMessage = "";
        var strCheckBoxName = "";
        var blnGrid = false;
        var strGridReplace = "";
        var intNumChecksDisplayed = 0;
        var CheckObj = 0;
        //If they have not specified a min or a max then any thing will work.
        if (intMin > -1 || intMax > -1) {
            //If the size of the list is less then the max then adjust the max
            if (ListArray.length < intMax) {
                intMax = ListArray.length;
            }
            //If the size of the list is less then the min then adjust the min
            if (ListArray.length < intMin) {
                intMin = ListArray.length;
            }
            //Figure out if it is from a grid or regular.
            if (strVarName.search(/_.\*/) > 0) {
                blnGrid = true;
            }
            for (i = 0; i < ListArray.length; i++) {
                if (blnGrid) {
                    strCheckBoxName = strVarName.replace(/\*/, ListArray[i]);
                } else {
                    strCheckBoxName = strVarName + "_" + ListArray[i];
                }
                CheckObj = document.mainform[strCheckBoxName];
                if (CheckObj) {
                    intNumChecksDisplayed++;
                    if (CheckObj.checked == true) {
                        intNumSelected++;
                        //Find out if None was checked and if so change intMin to 1
                        for (j = 0; j < NoneItemArray.length; j++) {
                            if (ListArray[i] == NoneItemArray[j]) {
                                if (intMin > 1) {
                                    intMin = 1;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if (intMin > intNumChecksDisplayed) {
                intMin = intNumChecksDisplayed;
            }
            if ((intNumSelected < intMin) || (intNumSelected > intMax)) {
                if (intNumSelected < intMin) {
                    strMessage = strGlobalError_min_check;
                } else {
                    strMessage = strGlobalError_max_check;
                }
                strMessage = SSI_ReplaceErrMsgKeyWords(strMessage, intQNum, strQName, strErrTxt, intMin, intMax);
                blnValid = false;
            }
        }
        if (blnGrid) {
            strVarName = strVarName.replace(/_r\*/, "");
            strVarName = strVarName.replace(/_c\*/, "");
        }
        SSI_UpdateQuestionErrHash(strVarName, strQName, strMessage, 0);
        return blnValid;
    }
</script>
answered Apr 2, 2021 by Justin Luster Silver Sawtooth Software, Inc. (8,300 points)
...