How to limit the amount of checkboxes selected?

2

I have form with 5 (five) checkbox . I would like the user to only be able to check three (3) of those five (5).

How do I do this validation using jQuery or Bootstrap?

Thanks everyone for your cooperation.

    
asked by anonymous 20.02.2015 / 06:03

1 answer

5

$(function(){
  var MAX_SELECT = 3; // Máximo de 'input' selecionados
  
  $('input.limited').on('change', function(){
    if( $(this).siblings(':checked').length >= MAX_SELECT ){
       this.checked = false;
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><p><strong>Selecione3opções:</strong></p><inputclass='limited'type='checkbox'/>OpçãoA<br><inputclass='limited'type='checkbox'/>OpçãoB<br><inputclass='limited'type='checkbox'/>OpçãoC<br><inputclass='limited'type='checkbox'/>OpçãoD<br><inputclass='limited'type='checkbox'/>OpçãoE<br>

Usefullink: .sibilings

    
20.02.2015 / 07:02