I am trying to set the minimum and maximum selection values ββfor a multiple selection field.
I saw something like this for a list of radio buttons (only for maximum value) here
http://jsfiddle.net/rREfg/1/
var maxCheckedCount = 5;
var maxCheckedAlertMessage = 'Woops! Too many selected!';
jQuery('input[type=checkbox]').click(function(){
var n = jQuery('input:checked').length;
if(n>=maxCheckedCount){
jQuery(this).prop('checked',false);
alert(maxCheckedAlertMessage);
}
});
Now if this is my new html
<select multiple size=7>
<option value="1">Strawberry</option>
<option value="2">Prune</option>
<option value="3">Lemon</option>
<option value="4">Peach</option>
<option value="5">Apple</option>
<option value="6">Banana</option>
<option value="7">Orange</option>
</select>
How can I tweak the code to get a minimum selection of 2 elements and a maximum selection of 5 elements?
source
share