multiple select checkbox

Discussions about CSEntry
Post Reply
mashour
Posts: 14
Joined: October 13th, 2015, 10:26 am

multiple select checkbox

Post 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
Last edited by mashour on August 3rd, 2016, 3:55 pm, edited 1 time in total.
Gregory Martin
Posts: 1796
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: multiple select checkbox

Post 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;
mashour
Posts: 14
Joined: October 13th, 2015, 10:26 am

Re: multiple select checkbox

Post 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;	
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: multiple select checkbox

Post 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;
mashour
Posts: 14
Joined: October 13th, 2015, 10:26 am

Re: multiple select checkbox

Post by mashour »

Hi,

Thanks for these really good suggestions! :D

M
MrTaco
Posts: 128
Joined: November 18th, 2014, 1:55 am

Re: multiple select checkbox

Post 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?
Attachments
2.png
2.png (29.29 KiB) Viewed 8495 times
mashour
Posts: 14
Joined: October 13th, 2015, 10:26 am

Re: multiple select checkbox

Post 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
Post Reply