PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

How Can I Limit the Number of Selections Made in a Checkbox Field

  1. FionaFell
    Member

    I have a need to limit the number of checkboxes that are chosen in a form.

    EXAMPLE:
    (A) Choose 1 category
    (B) Choose up to 3 categories

    If the user chooses (A), so they can only pick one category, from a drop down box.

    What I need to do, is limit the number of selections if they choose (B) as I'm displaying a checkbox list of categories. If I have 72 categories, I want them to only be able to choose a max of 3 (three), not all 72.

    Is this possible? What is the easiest way to implement this?

    Posted 6 years ago on Monday April 9, 2012 | Permalink
  2. black_lady
    Member

    Hi, this solution doesn't work for me. I put this code in functions.php and changed input ID, and nothing. Any idea?
    code i put:
    <script type="text/javascript">
    jQuery.noConflict();
    jQuery(document).ready(function($) {

    $.fn.limit = function(n) {
    var self = this;
    this.click(function(){ return (self.filter(":checked").length<=n); });
    }
    $("ul#input_22_70 li input:checkbox").limit(2);

    });
    </script>

    Posted 5 years ago on Monday March 4, 2013 | Permalink
  3. black_lady
    Member

    Solution from http://gravitywiz.com/2012/06/11/limiting-how-many-checkboxes-can-be-checked/
    is works!
    I put this code into functions.php (at the end) and I recived error.
    But when I put this code on the begining - everythig fine!
    Thanks David :)

    Posted 5 years ago on Tuesday March 5, 2013 | Permalink
  4. Thank you for the update. When it was added to the end, you probably had an issue with an extra or missing <?php or ?> tag. When you inserted it in the beginning, you got it properly in a PHP block. Glad that is working for you now.

    Posted 5 years ago on Wednesday March 6, 2013 | Permalink