Page 1 of 1
multiple select checkbox
Posted: July 28th, 2016, 11:21 am
by mashour
Hi,
Supposed I have question with three options and I want the user to be able to select as many as applicable, how do I prevent them from entering invalid response values? So for instance in the example below, how do I prevent the user from entering "D" which is not one of the three options or from entering something like "AA"?
A=Today
B=Yesterday
C=Last week
I suppose I could have a check in the postproc that ensures that pos("D",source) is 0, and if not then it displays an error message. But how do I make sure that some responses are not selected twice? For example, "ABA" or "AAB" or "CAA". With three response options only, the pos() function can do this, but with 10 or more options this seems tedious/impossible. How have others dealt with this?
M
Re: multiple select checkbox
Posted: July 28th, 2016, 11:12 pm
by Gregory Martin
It can be annoying to work with checkboxes in CSPro, but you could do something like this to make sure that there aren't duplicate selections, assuming that each checkbox entry is one character.
PROC CHECKBOX
numeric ctr;
do ctr = 2 while ctr <= strip(length(CHECKBOX))
if CHECKBOX[ctr:1] = CHECKBOX[( ctr - 1 ):1] then
errmsg("You cannot select %s twice.",CHECKBOX[ctr:1]);
reenter;
endif;
enddo;
Re: multiple select checkbox
Posted: July 29th, 2016, 6:39 pm
by mashour
Thanks for the suggestion! I think you meant
length(strip(CHECKBOX)) instead of
strip(length(CHECKBOX)) below. Your solution works well if the repeated selections are back to back such as AAB or CAA, but not if you enter ABA. I ended up resorting to the ridiculously long solution below to guarantee that they enter at least one valid alpha character. I think my solution would be super tedious if the choice set was large, so I welcome any improvements or suggestions for the future!
Code: Select all
PROC GLOBAL
array alpha (1) choices(10);
PROC CHECKBOX
postproc
numeric ctr;
numeric check = 0;
numeric j =1 ;
choices(1)="A";
choices(2)="B";
choices(3)="C";
choices(4)="D";
choices(5)="E";
choices(6)="F";
choices(7)="G";
choices(8)="H";
choices(9)="I";
choices(10)="J";
// this is to make sure they don't enter any choice more than once
while j <=10 do
check =0;
do ctr = 1 while ctr <= length(strip(CHECKBOX))
if CHECKBOX[ctr:1] = choices(j) then
if check <> 1 then
check = 1;
else
errmsg("You cannot select option %s twice. Please review your selections.",CHECKBOX[ctr:1]);
reenter;
endif;
endif;
enddo;
j=j+1;
enddo;
// this is to make sure they don't enter any numerical values
do ctr = 1 while ctr <= length(strip(CHECKBOX))
if CHECKBOX[ctr:1] = "1" or CHECKBOX[ctr:1] = "2" or CHECKBOX[ctr:1] = "3" or CHECKBOX[ctr:1] = "4" or CHECKBOX[ctr:1] = "5" or CHECKBOX[ctr:1] = "6" or CHECKBOX[ctr:1] = "7" or CHECKBOX[ctr:1] = "8" or CHECKBOX[ctr:1] = "9" then
errmsg("You cannot enter a number. Please use letters to indicate your choice.");
reenter;
endif;
enddo;
// this is to make sure they don't leave the space blank
if length(strip(CHECKBOX))=0 then
errmsg("You have not entered anything. Please enter a valid selection.");
reenter;
endif;
Re: multiple select checkbox
Posted: August 1st, 2016, 7:36 am
by josh
You could simplify the code a little by using the pos function:
numeric i;
do i = 1 while i < length(strip(CHECKBOX))
if pos(CHECKBOX[i:1], CHECKBOX[i+1]) then
errmsg("You cannot select option %s twice. Please review your selections.",CHECKBOX[i:1]);
reenter;
endif;
enddo;
Instead of checking for numeric entries you can use the invalueset() function to make sure that each selection is one of the options in the value set which, assuming your value set only contains letters, will eliminate numeric choices:
// Ensure that the values picked are in the value set
do i = 1 while i <= length(strip(CHECKBOX))
if not invalueset(CHECKBOX[i:1]) then
errmsg("%s is not a valid option. Please review your selections.",CHECKBOX[i:1]);
reenter;
endif;
enddo;
Re: multiple select checkbox
Posted: August 3rd, 2016, 3:36 pm
by mashour
Hi,
Thanks for these really good suggestions!
M
Re: multiple select checkbox
Posted: August 26th, 2016, 4:26 am
by MrTaco
Good day
Code: Select all
if pos("1,2,3,4,5,6,7,8,9,10",HH_Q2) in 1:10 then
skip to HH_Q3; endif;
do i = 1 while i < length(strip(HH_Q2))
if pos(HH_Q2[i:1], HH_Q2[i+1]) then
errmsg("You cannot select option %s twice. Please review your selections.",HH_Q2[i:1]);
reenter;
endif;
enddo;
I used this logic for mutliple checkbox but it doesnt allow 1 and 10 as different numbers and it doesnt skip to variable?
Re: multiple select checkbox
Posted: August 26th, 2016, 12:32 pm
by mashour
Hi,
If you browse several old posts you will see that it's really not recommended to use numbers as response choices for multiple select questions (especially if you have more than 9 choices). Right now 1 and 10 are being read as 0 selected once and 1 selected twice, hence the error. You can use letters of the alphabet instead, such as A through J. You can see an example in my original post and the replies to it above.
M