Is it possible to set limit to multipleselect?
Below is an example code where user can select more than 1 value.
But, how to limit user to select not more than 3 value. Any idea?
Is it possible to set limit to multipleselect?
Below is an example code where user can select more than 1 value.
But, how to limit user to select not more than 3 value. Any idea?
You can use jQuery
$("select").change(function () { if($("select option:selected").length > 3) { //your code here } });
You would do this via javascript on the client side, and then add a check on the server side as well in case the client has disabled javascript.
Here is some basic client side code to give you an idea:
Script that will disallow more than 3 elements to be selected (as opposed to just validating it at submit time).
var verified = []; document.querySelector('select').onchange = function(e) { if (this.querySelectorAll('option:checked').length -1; }); } }
Note there's some additional complexity related to preventing the item from actually being selected on the select
and that it rather validates a user's actions and reverts it if it's invalid.
RedFilter has the right idea with validating by javascript. Haven't tested, but you could do something like:
var options = document.all.tags("option");
var selectedCounter = 0; for (var i=0; i