views:

15

answers:

0

I have set up a multiselect select box and am successfully limiting selection to (say) 5 elements. I can detect when the max number is exceeded, but I can't work out how to roll back to the previous selection so as to be friendly to the user.

I tried to return false, but that didn't work. I don't want to try to remember the last valid selection and roll back to that, because that could get ugly.

               $("select[multiple='multiple']").change(function(){
                    var $max = 5;
                    var $count = 0;
                    $("option:selected", $(this)).each(function () 
                    {
                        $count++;
                    });

                   if ($count > $max) 
                   {                            
                        alert ("You have selected " + $count + " items but the maximum number is " + $max);
                        return false;                       
                   }
            });