Limiting the number of entries in a checkbox

fabrikThis is quite tricky but can be solved by using some jQuery on the appropriate form.


Scenario

You have 10 options in a checkbox.  By default visitors can select, all, none or some of them.  Suppose you only want visitors to be able to choose a maximum of 5.  For this example I have a databasejoin rendered as a Checkbox.  The element is called votes, and it is in the fab_votes table, hence the fullname of the element is fab_votes___votes.

 

Form

Locate the form that the element is being used in.

  1. Log into the backend of Joomla.
  2. Go to Components > Fabrik > Forms.
  3. Search for the Form.
  4. Make a note of the ID number (the first column number, e.g. 39).

 

jQuery code

Now you can create the code using the Form ID number as part of the filename.

  1. Using your preferred file manager create the following file:
  2. /components/com_fabrik/js/form_39.js where 39 is the ID number of the form.
  3. Open the file for editing and add the following content:
  jQuery('#fab_votes___votes').on('click', '.fabrikinput', function(e) {
if (jQuery("input[name^='fab_votes___votes']:checked").length > 5) {
alert("You can have a maximum of 5 votes");
this.checked = false;
}
});

Now when a visitor tries to tick a 6th checkbox they get a dialog box stating 'You can have a maximum of 5 votes' and their last selection becomes unselected.

Note that you may need to clear the cache to see the effect of this jQuery on the form.

 

Thanks for visiting.