title: check if only one checkbox is checked
body: <p>I have seven checkboxes, like this:</p>
  
  <pre><code>&lt;div class="contact-label span11 rights"&gt;
            &lt;div class="row-fluid"&gt;
              &lt;div class="span3"&gt;
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canorder_0" id="contactpersonen_canorder_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canorder_0"&gt;Mag border plaatsen&lt;/label&gt;
  
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseestock_0" id="contactpersonen_canseestock_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseestock_0"&gt;Mag gderen afhalen&lt;/label&gt;
              &lt;/div&gt;
              &lt;div class="span3"&gt;
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseeorders_0" id="contactpersonen_canseeorders_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseeorders_0"&gt;Mag orders inzien&lt;/label&gt;
  
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canaddaddress_0" id="contactpersonen_canaddaddress_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canaddaddress_0"&gt;Mag bezorgadressen aanpassen&lt;/label&gt;
              &lt;/div&gt;
              &lt;div class="span3"&gt;
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseeprice_0" id="contactpersonen_canseeprice_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseeprice_0"&gt;Mag netto prijs zien&lt;/label&gt;
  
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseecredit_0" id="contactpersonen_canseecredit_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseecredit_0"&gt;Mag facturen en creditfacturen inzien&lt;/label&gt;
              &lt;/div&gt;
              &lt;div class="span3"&gt;
                &lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseepickup_0" id="contactpersonen_canseepickup_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseepickup_0"&gt;Mag wijzigingen doorgeven&lt;/label&gt;
              &lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>and I have a javascript function for checking duplicate email, like this:</p>
  
  <pre><code>$.validator.addMethod("contactpersonen-email", function(value, element) {
      var parentForm = $(element).closest('form');
      var timeRepeated = 0;
      if (value != '') {
          $(parentForm.find(':text')).each(function () {
              if ($(this).val() === value) {
                  timeRepeated++;
              }
          });
      }
  
      var ok = timeRepeated === 1 || timeRepeated === 0;
      if(!ok) alert('Emaild adres bestaat al');
      return ok;
  }, "Email adres bestaat al");
  </code></pre>
  
  <p>But now if only this checkbox is checked:</p>
  
  <pre><code>&lt;input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseestock_0" id="contactpersonen_canseestock_0"&gt;&lt;/input&gt;
                &lt;label class="checkbox" for="contactpersonen_canseestock_0"&gt;Mag gderen afhalen&lt;/label&gt;
  </code></pre>
  
  <p>Then is has to skip the email duplicate function</p>
  
  <p>But what is the easiest way to check for that? Or you have to check if every other checkbox is not checked?</p>
  
  <p>Thank you</p>
  
  <p>this is a screenshot for cleareness:</p>
  
  <p><a href="https://i.stack.imgur.com/8wPbx.png" rel="nofollow"><img src="https://i.stack.imgur.com/8wPbx.png" alt="enter image description here"></a></p>
  
  <p>Oke, but if I do it like this:</p>
  
  <pre><code>$.validator.addMethod("contactpersonen-email", function(value, element) {
      var parentForm = $(element).closest('form');
      var timeRepeated = 0;
      if (value != '') {
          $(parentForm.find(':text')).each(function () {
              if ($(this).val() === value) {
                  timeRepeated++;
              }
          });
      }
  
  
  
  
  
  
   //
  $("#contactpersonen_canseestock_0").attr('checked', true);
  //Alleen een is checked en dat is Mag gderen afhalen
  if ($("#contactpersonen_canseestock_0").is(":checked") &amp;&amp; $('#formid :checkbox:checked').length == 1) {
    alert("checked");
  } else {
    // any other check box is checked or none
    alert("other combination");
  }
  
  //
      var ok = timeRepeated === 1 || timeRepeated === 0;
      if(!ok) alert('Emaild adres bestaat al');
      return ok;
  }, "Email adres bestaat al");
  </code></pre>
  
  <p>it goes only in:  any other check box is checked or none</p>
  
  <p>see image:
  <a href="https://i.stack.imgur.com/vr7LQ.png" rel="nofollow">enter image description here</a></p>
  
  <p>If I try it like this:</p>
  
  <pre><code>$.validator.addMethod("contactpersonen-email", function(value, element) {
      if($("#contactpersonen_canseestock_0").is(':checked')) {
      console.log('email is nice');
      var parentForm = $(element).closest('form');
      var timeRepeated = 0;
      if (value != '') {
          $(parentForm.find(':text')).each(function () {
              if ($(this).val() === value) {
                  timeRepeated++;
              }       
          });
      }
      var ok = timeRepeated === 1 || timeRepeated === 0;
      if(!ok) alert('Emaild adres bestaat al');
      return ok;
  }else{
  console.log('jkshdfkjsdhf');
  return;
  }}, "email bestaat al" )
  </code></pre>
  
  <p>it doesnt work. see image:</p>
  
  <p><a href="https://i.stack.imgur.com/LyVGG.png" rel="nofollow"><img src="https://i.stack.imgur.com/LyVGG.png" alt="enter image description here"></a></p>
  
  <p>oke. I have it now like this:</p>
  
  <pre><code>$.validator.addMethod("contactpersonen-email", function(value, element) {
      var parentForm = $(element).closest('form');
      var timeRepeated = 0;
      if (value != '') {
          $(parentForm.find(':text')).each(function () {
              if ($(this).val() === value) {
                  timeRepeated++;
              }
          });
      }
  
   //
  
  
  //
      var ok = timeRepeated === 1 || timeRepeated === 0;
      $("#contactpersonen_canseestock_0").attr('checked', true);
  //Alleen een is checked en dat is Mag gderen afhalen
  if ($("#contactpersonen_canseestock_0").is(":checked") &amp;&amp; $('#formid :checkbox:checked').length == 1) {
    console.log("checked");
      return;
  } else {
      if(!ok) alert('Emaild adres bestaat al');
      return ok;
  
    // any other check box is checked or none
    console.log("other combination");
  }   
  
  
  
      //if(!ok) alert('Emaild adres bestaat al');
      //return ok;
  }, "Email adres");
  </code></pre>
  
  <p>so I see it comes int:</p>
  
  <pre><code>console.log("checked");
  </code></pre>
  
  <p>. But it also comes her in:</p>
  
  <pre><code> }, "Email adres");
  </code></pre>
  
  <p>What ofcourse not has to be.</p>
  
  <p>maybe it is coming because I have above in the file this:</p>
  
  <pre><code>contactpersonen_email1:{
                                          required:true,
                                          email:true
                                          },  
  </code></pre>
  
  <p>But it is then not required ofcourse if the first checkbox is checked.</p>
  
  <p>Thank you   </p>
  
  <p>and if do it like this:</p>
  
  <pre><code>if ($("#contactpersonen_canseestock_0").is(":checked") &amp;&amp; $('#formid :checkbox:checked').length == 1) {
    console.log("checked");
      return;
  } else {
      if(!ok) alert('Emaild adres bestaat al');
      console.log('not checked');
      return ok;
  
    // any other check box is checked or none
    console.log("other combination");
  }   
  
  
  return;
      //if(!ok) alert('Emaild adres bestaat al');
      //return ok;
  });
  </code></pre>
  
  <p>so this:</p>
  
  <pre><code>   });  then I will get this warning:
  </code></pre>
  
  <p>Warning: No message defined for contactpersonen_email</p>
  
  <p>How to combine this method:</p>
  
  <pre><code>$('#formid :checkbox').change(function() {
  //only one is checked and that is Mag gderen afhalen
  if ($("#contactpersonen_canseestock_0").is(":checked") &amp;&amp; $('#formid :checkbox:checked').length == 1) {
    console.log("only Mag gderen afhalen checked");
  } else {
    // any other check box is checked or none
    console.log("other combination");
  }});
  </code></pre>
  
  <p>with this method:</p>
  
  <pre><code>$.validator.addMethod("contactpersonen-email", function(value, element) {
      var parentForm = $(element).closest('form');
      var timeRepeated = 0;
      if (value != '') {
          $(parentForm.find(':text')).each(function () {
              if ($(this).val() === value) {
                  timeRepeated++;
              }
          });
      }
  
  
      var ok = timeRepeated === 1 || timeRepeated === 0;
      if(!ok) alert('Emaild adres bestaat al');
      return ok;
  }, "Email adres bestaat al");
  </code></pre>
  
  <p>Thank you</p>