The select_multiple field type allows you to create a multiple choice question for which respondents can select multiple answers at the same time. But there are two very common scenarios where you may want to restrict the number of answer options that can be selected:
1) You may only want to limit the number of answers to be selected. For example, perhaps you have a question where a user can select up to three responses - but there are a total of 10 possible answer options.
In that case how do you restrict the total number of answers that can be selected while still allowing multiple responses?
You can use the count-selected() function. In the constraint column for the select_multiple field, use a version of the following expression:
count-selected(.) <= 3
The above expression will allow 3, or fewer, responses.
2) One of the answer options might be "refuse" or "don't know". In that case, you may want to allow multiple responses if any other answer options are selected, but if "refuse" or "don't know" are selected, you will only want to allow one answer option to be chosen. In every day language, the logic we want is: "IF "refuse" is selected, only allow one answer ELSE allow more than one answer". How to translate this into an actual constraint expression?
You can use a combination of the count-selected() function and the if() function [below I have assumed that the internal numeric value for "refuse" in the choice list is -99]:
if(selected(., -99), count-selected(.) = 1, count-selected(.) >= 1)
Now, if refuse is selected, the respondent will not be permitted to select any other answer options. In contrast, if refuse is not selected as one of the answer options, the respondent can select as many of the other answer choices as they want.
Further, if you happen to have "refused" and "don't know" in the same choice list and wish to prevent answers that include either in combination with other answers, the following will work:
if(selected(., -99) or selected(., -88), count-selected(.) = 1,
count-selected(.) >= 1)
See that another selected() condition was added in the condition for if()? You can extend this expression in the above way.
As a note and extension ot this article, there are many ways of reaching the same objectives in SurveyCTO given that expression are very flexible. An alternative to the first solution above, to ensure that -99 is selected by itself is the following:
not(selected(., -99) and count-selected(.) > 1)
What this expression is saying is that "inside the current field, -99 is not allowed to be selected when the number of selections is greater than 1". Alternatively, to prevent -99 or -88 from being selected in combination with other answers, you might extend the above logic as follows:
not((selected(., -99) or selected(., -88)) and count-selected(.) > 1)
You now know two different methods for logically constraining select_multiple field responses! For more on constraints, see our guide to constraints.
Functions and operators referenced in this article: selected(), if(), count-selected(), not(), =, <, >. For a full list of SurveyCTO functions and operators, please read this help guide.
Do you have thoughts on this support article? We'd love to hear them! Feel free to fill out this feedback form.