Limit the total of options in select - PHP

2

How to limit the number of options selected in a select so that the form is not sent above a maximum amount.

For example, I want a maximum of 3 options to be sent on the form. Above that, I wanted to prevent the user from submitting the form.

How can I do this?

<form action="" method="post">
<select class="form-control" name="options" multiple="multiple">
    <option value="1">Opção 1</option>
    <option value="2">Opção 2</option>
    <option value="3">Opção 3</option>
    <option value="4">Opção 4</option>
    <option value="5">Opção 5</option>
    <option value="6">Opção 6</option>
    <option value="7">Opção 7</option>
    <option value="8">Opção 8</option>
    <option value="9">Opção 9</option>
    <option value="10">Opção 10</option>
</select>
<input type="submit" name="send" value="enviar">
</form>
    
asked by anonymous 21.09.2016 / 14:42

2 answers

1

Improving the @Ricardo code a bit ... Here it disables the button when it reaches more than 3 selected.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><scripttype="text/javascript">
$(document).ready(function(){
  $("#options").change(function(){
    var total = $("#options :selected").length;

   if(total > 3){
     $("#send").prop("disabled", true);
   }else{
     $("#send").prop("disabled", false);
   }
  });
});
</script>
<form action="" method="post">
<select class="form-control" id="options" name="options" multiple="multiple">
    <option value="1">Opção 1</option>
    <option value="2">Opção 2</option>
    <option value="3">Opção 3</option>
    <option value="4">Opção 4</option>
    <option value="5">Opção 5</option>
    <option value="6">Opção 6</option>
    <option value="7">Opção 7</option>
    <option value="8">Opção 8</option>
    <option value="9">Opção 9</option>
    <option value="10">Opção 10</option>
</select>
<input type="submit" id="send" name="send" value="enviar">
</form>
    
21.09.2016 / 15:11
4

You can do this by javascript: Look at this Fiddle

$( "#send" ).click(function() {
  var count = $("#options :selected").length;
  alert( count );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><formaction="" method="post">
<select class="form-control" id="options" name="options" multiple="multiple">
    <option value="1">Opção 1</option>
    <option value="2">Opção 2</option>
    <option value="3">Opção 3</option>
    <option value="4">Opção 4</option>
    <option value="5">Opção 5</option>
    <option value="6">Opção 6</option>
    <option value="7">Opção 7</option>
    <option value="8">Opção 8</option>
    <option value="9">Opção 9</option>
    <option value="10">Opção 10</option>
</select>
<input type="submit" id="send" name="send" value="enviar">
</form>

Running the above code is also possible to see the result.

I'm not blocking sending, but if you select multiple items the alert will display how many.

Just try and use javascript as you see fit.

    
21.09.2016 / 14:59