Restrict checkbox group

janman 30 Mar, 2016
I would like to know if you can restrict a users using checkbox group. I have 25 product that they can choose form but they can only select 21. Is this possible with chronoforms?
GreyHead 30 Mar, 2016
Hi Janman,

Yes it is possible - but needs some custom JavaScript to do it.Here's the code from a similar project limiting the number of 'files' that can be selected with checkboxes
jQuery(document).ready(function (jQ) {
  var files;
  files = jQ('input[name="file_select[]"]');
  files.on('click', checkCount);

  function checkCount() {
    var files_checked;
    files_checked = jQ('input:checkbox[name="file_select[]"]:checked');
    if ( files_checked.length >= 5 ) {
      jQ('#complete').css('visibility', 'visible');
      files.each(function (){
        if ( !jQ(this).prop('checked') ) {
          jQ(this).prop('disabled', true);
        }
      });
    } else {
      jQ('#complete').css('visibility', 'hidden');
      files.each(function () {
        jQ(this).prop('disabled', false);
      });
    }
  }
});
What this does is to disable all the un-checked boxes once five have been checked, if one of those is un-checked then they are all re-enabled.

Bob
janman 31 Mar, 2016
Thanks bob for the quick response. i am looking to do the following
http://www.plus2net.com/javascript_tutorial/checkbox-limit.php
http://www.plus2net.com/javascript_tutorial/checkbox-limit-demo.php
Thanks.
Jan
janman 31 Mar, 2016
Hi Bob
i Found this
http://www.chronoengine.com/forums/posts/f5/t17581.html
will it work with V5?
GreyHead 31 Mar, 2016
Hi janman,

What do those examples do that is different from the code I have already posted?

Bob
janman 04 Apr, 2016
Hi Bob
Please check i have added the JavaScript code with no luck
http://1021dev.co.za/ladybug/index.php?option=com_chronoforms5&chronoform=Products
Thanks
Jan
GreyHead 04 Apr, 2016
Hi Jan,

It looks as if you haven't edited to code to match the name of the Checkbox group in your form.

Bob
janman 04 Apr, 2016
Hi Bob
check box group name is "checkbox_group1"
Am i missing something else?

    jQuery(document).ready(function (jQ) {
      var files;
      files = jQ('input[name="checkbox_group1[]"]');
      files.on('click', checkCount);

      function checkCount() {
        var files_checked;
        files_checked = jQ('input:checkbox[name="checkbox_group1[]"]:checked');
        if ( files_checked.length >= 5 ) {
          jQ('#complete').css('visibility', 'visible');
          files.each(function (){
            if ( !jQ(this).prop('checked') ) {
              jQ(this).prop('disabled', true);
            }
          });
        } else {
          jQ('#complete').css('visibility', 'hidden');
          files.each(function () {
            jQ(this).prop('disabled', false);
          });
        }
      }
    });
GreyHead 04 Apr, 2016
Hi Jan,

Actually it looks as if the name is check0

Bob
janman 04 Apr, 2016
Thanks bob
You are a rockstar
This topic is locked and no more replies can be posted.