title: allow a minimum selection in a checkboxes limiting script
body: <p>I have a javascript code that limits checkbox selection.</p>
  
  <p>It's taking the number of checkboxes in a given page and allows only 30% of them to be selected.</p>
  
  <p>This is the code:</p>
  
  <pre><code>function chkcontrol(j) {
    var length = document.forms['form1'].elements['item[]'].length;
  
    var max = Math.round(length * 0.3);
    var total = 0;
    for(var i = 0; i &lt; length; i++){  
      if(document.forms['form1'].elements['item[]'][i].checked){
        total = total + 1;
      }
      if(total &gt; max){
        alert("MAX 33%") ;
        document.forms['form1'].elements['item[]'][i].checked = false ;
        return false;
      }
    }
  }
  </code></pre>
  
  <p>It works fine, but I would like to set an absolute minimum guaranteed number of checkboxes selectable.</p>
  
  <p>In simple text, here is the rule I am trying to implement:</p>
  
  <ul>
  <li>Allow 30% of the checkboxes AND if the 30% limit is allowing less than 5 checkboxes --> allow 5.</li>
  </ul>
  
  <p>Example:</p>
  
  <p>7 checkboxes in the page, only 2 allowed. (limit is 2, which is less than the minimum guaranteed of 5 --> the script should raise the limit to allow 5)</p>
  
  <p>Any ideas how to do this?
  In php a simple IF condition would solve the problem, but I am not familiar with javascript!</p>