Limiting How Many Checkboxes Can Be Checked

A simple method for limiting how many checkboxes can be checked (and how many checkboxes must be checked) on a checkbox field.

February 9, 2013: Updated to only apply validation on currently submitted page for multi-page forms.

Stop! There's a better way.

This snippet is available as a plugin with Gravity Perks, a suite of over 46 premium Gravity Forms plugins!

View Plugin Buy Gravity Perks

The snippet might look daunting but it only requires a quick copy and paste and two simple configurations to get it up and running!

How do I install this snippet?

Just copy and paste the code into your theme’s functions.php file.

Do I need to modify this snippet to work with my form?

This snippet now uses a class format. This means you can more easily use it on multiple forms! To instantiate this class you will need to know the form ID, field ID and minimum/maximum number of checkboxes which should be allowed/enforced for that field. It will look something like this:

new GFLimitCheckboxes(123, array(
	5  => array(
		'min' => 2,
		'max' => 3,
	),
));

In template form:

new GFLimitCheckboxes(FORM_ID, array(
	FIELD_ID => array(
		'min' => MIN_NUMBER,
		'max' => MAX_NUMBER,
	),
));

You can specify multiple checkbox fields on a specific form like so:

new GFLimitCheckboxes(123, array(
	5  => array(
		'min' => 2,
		'max' => 3,
	),
	13 => array(
		'max' => 3,
	),
));

Finally, you can apply this functionality to multiple forms:

new GFLimitCheckboxes(123, array(
	5  => array(
		'min' => 2,
		'max' => 3,
	),
	13 => array(
		'max' => 3,
	),
));
new GFLimitCheckboxes(321, array(
	1  => array(
		'max' => 4,
	),
));

And that’s it!

You can use a little CSS to style the labels for the disabled checkboxes. Here is an example where I’ve set the color to gray: input[type="checkbox"]:disabled + label { color: #999; }

Comments

  1. Kyle Hart
    Kyle Hart March 3, 2023 at 7:45 pm

    Will this work with Gravity forms 2.7.2 & multiple fields? The order of the code doesn’t seem to matter. Whatever the first field is on the displayed form, works fine, then the following fields behave as if nothing was written for them.

    Reply
    1. David Smith
      David Smith Staff March 6, 2023 at 9:12 pm

      Tested with GF 2.7.2 and multiple fields – both min and max limits were honored for both fields.

      If you’re a Gravity Perks customer, you might consider using the perk version of this, GP Limit Checkboxes or contacting support for a deeper dive if you need to use this snippet version for some reason.

    1. Samuel Bassah
      Samuel Bassah Staff October 3, 2022 at 4:32 am

      Hi Carlos,

      If I understand correctly, you want to set the number of options that populates a dropdown field? If so then you can use this gppa_query_limit filter hook to set the number of values that are returned when using GP Populate Anything. You can use the last example on the hook documentation that targets a specific field on a form to do what you want. If this isn’t what you want and I have misunderstood your question, please let us know. You can get in touch with us via our support form.

      Best,

    2. Carlos
      Carlos October 3, 2022 at 7:09 am

      Samuel! Hello! No, I don’t want to limit the number of queries, but I do want to limit the number that can be selected once the query brings me the options.

    3. Carlos
      Carlos October 3, 2022 at 10:11 am

      Hello Samuel, I have tried that Perk. It works ok with checkboxes, but not with the Selector module. Is it possible to implement it in selector? Thank you!

    4. Samuel Bassah
      Samuel Bassah Staff October 3, 2022 at 10:17 am

      Hi Carlos,

      What exactly is a selector module? If you have an active Gravity Perks license, can you get in touch with us via our support form and send an export of the form, so we can take a look at the setup and assist you further?

      Best,

    5. Samuel Bassah
      Samuel Bassah Staff October 3, 2022 at 2:58 pm

      Hi Carlos,

      Yes, I can confirm the snippet currently doesn’t work on mobile. I’m forwarding this to our developers to look into this and see if mobile support can be added to the snippet. One of our developers will update this comment with an update on this.

      Best,

  2. Tim
    Tim September 5, 2022 at 5:37 am

    Hi Samuel,

    Thnx for the code! Works fine, I just want to know how to apply more form IDs and more field IDs in the code? Now I make the same code for each field. And also want to use the code for more more forms.

    This didn’t work:

    26,28,29 => array( ‘min’ => 2, ‘max’ => 2 )

    The code how it was used for 1 form with the same settings for all fields.

    new GFLimitCheckboxes(2, array( 26 => array( ‘min’ => 2, ‘max’ => 2 ), 28 => array( ‘min’ => 2, ‘max’ => 2 ), 29 => array( ‘min’ => 2, ‘max’ => 2 ) ));

    Reply
    1. Dario Space
      Dario Space September 6, 2022 at 10:15 am

      Hi Tim,

      This will require some adaptation to the snippet. If you have an active Gravity Perks license, please get in touch through our support form, and we’ll dig into it.

      Another option would be migrating to Limit Checkboxes perk.

      Best,

    1. Samuel Bassah
      Samuel Bassah Staff August 19, 2022 at 6:41 am

      Hi Jeremy,

      The configuration is at the button of the code, similar to this;

      new GFLimitCheckboxes(115, array( 5 => array( 'min' => 2, 'max' => 3 ), 13 => array( 'max' => 3 ) ));

      The form ID in the code above is 115 and the field IDs are 5 and 13.

    2. Jeremy
      Jeremy September 2, 2022 at 2:11 am

      Hi Samuel,

      Thanks for replying, I tried the below as I only have one check box that needs the code however doesn’t seem to be working.

      new GFLimitCheckboxes(4, array( 135 => array( ‘min’ => 2, ‘max’ => 3 ) ));

    3. Samuel Bassah
      Samuel Bassah Staff September 2, 2022 at 7:25 am

      Hi Jeremy,

      I just did a test using your configuration, by just changing the Form and Field IDs and it worked, so it’s strange it’s not working for you. Does it display any error message? You may want to try it again but instead of copying and pasting, type the configuration part of the code. If you have an active Gravity Perks license you can get in touch with us via our support form and we’ll be happy to look into this further.

      Best,

  3. Mel
    Mel July 28, 2022 at 6:39 am

    Hi folks,

    Wondering if you can help, but appreciate it’s outside the remit – the snippet works perfectly for me if you’re logged in but doesn’t seem to work for logged out users. Any idea why that might be?

    I’m getting a jQuery error: .checkboxLimit is not a function in the console when the form loads in while in incognito that doesn’t appear when signed in. I see elsewhere you were asking about page builders – the site uses Elementor.

    Thanks for any insights!

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff July 28, 2022 at 7:01 am

      Hi Mel,

      Yes, this is often a caching-related issue. Glad you were able to get it working.

      Best,

  4. Razi Zulkepli
    Razi Zulkepli June 18, 2022 at 8:14 am

    Does not work. Code snippet plugin throes error “Uncaught Error: Class “GFLimitCheckboxes” not found in ………/public_html/wp-content/plugins/code-snippets/php/admin-menus/class-edit-menu.php(248) : eval()’d code:1…….

    Directly adding this snippet in functions.php causes my entire site to crash

    Reply
  5. Haya
    Haya June 14, 2022 at 5:06 am

    Is there anything similar to Multi-select?? I have a multi-select with 5 options, and I want the user to be able to ONLY choose 2 options.

    Reply
  6. Jonathan T
    Jonathan T May 9, 2022 at 5:43 pm

    The snippet works great. One question is if the user clicks a box now, if they want to switch they have to un-click the current before they can switch. Is there a way to allow the user to click and have it automatically switch?

    Thanks!

    Reply
    1. Dario Space
      Dario Space May 9, 2022 at 5:46 pm

      Thanks! Hmm, this is a good question. From what I understand as you mentioned is not currently supported. If you have a Gravity Perks license, can you get in touch with us via our support form?

      Best,

    2. Jonathan T
      Jonathan T May 9, 2022 at 6:07 pm

      I was using the code snippet, not a perk. I figured it had something to do with not disabling the other options when one is clicked?

    3. Dario Space
      Dario Space May 10, 2022 at 10:42 am

      Jonathan,

      The behavior you are experiencing is by design. If you only want to let the user select one option, you can use a Radio Buttons field instead 🙂.

      Best,

    1. Scott Ryer
      Scott Ryer Staff February 16, 2022 at 1:50 pm

      Hi Cory,

      I am unable to recreate the issue on my end. Unchecking boxes is enabling disabling boxes with GF 2.5.16. If you have a Gravity Perks license, reach out via the support form so we can assist you further with this.

  7. Scott Sibson
    Scott Sibson June 15, 2021 at 5:54 am

    Works a treat for me on GF 2.5, only issue I currently have is that jQuery is throwing a console error, not entirely sure why as jQuery is loaded before this script in my functions.php

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff June 15, 2021 at 10:38 am

      Hi Scott,

      I am unable to recreate the issue on my end. It works as expected without a JQuery error. I’ll suggest you run a Theme/plugin conflict test to check if another plugin or code is causing this. If you have a Gravity Perks license you can get in touch with us via support form so we can assist you further with this.

      Best,

    1. Dario Space
      Dario Space May 18, 2021 at 10:49 am

      Hi Chad,

      I’m unable to recreate the issue when I test on GF 2.5. It works as expected.

      We recommend purchasing the Perk version of this snippet. Not only does it offer more features and ease of use, it also includes support.

  8. Katie Hildreth
    Katie Hildreth February 27, 2020 at 3:46 pm

    Just wondering if this code be used to set a limit for the total items checked in a combination of three fields? So limit of 5 total for Field1+Field2+Field3…

    Reply
  9. Paul Webb
    Paul Webb February 7, 2020 at 5:39 am

    Great little piece of code but it seems to be stopping users not logged in from being able to add items to the cart (well it says they have been added but going to the cart page its empty)? The code all works as expected with the checkboxes and it works fine if you are logged in.

    I removed the code and it allows me to add to cart if not logged in.

    Reply
  10. Paula
    Paula December 10, 2019 at 7:48 am

    Hi,

    I put the code on my functions.php and appear a fatal error. I will show you what i paste:

    Estoy copiando lo siguiente: new GFLimitCheckboxes(4, array( 21 => array( ‘min’ => 1, ‘max’ => 5 ), 23 => array( ‘min’ => 1, ‘max’ => 5 ) ));

    The number 4 is my ID form and the 21 and 23 is the ID field. I donÂŽt understand if apart of the gravityÂŽs plugin i need to do something else.

    Reply
  11. Nathan Shepherd
    Nathan Shepherd March 7, 2019 at 6:56 am

    Awesome snippet, thanks David!

    For anyone experiencing issues: I had to make this a plugin in order for it to work. I couldn’t get it working just pasting everything in the functions.php, the code wouldn’t output the script to the page containing the form at all. I would recommend the same approach for anyone experiencing the same issue.

    Reply
    1. David Smith
      David Smith Staff March 7, 2019 at 10:40 am

      There may have been a formatting issue in your functions.php file but glad the plugin solution works for you. ?

  12. Dick Raney
    Dick Raney February 8, 2018 at 8:13 pm

    I’m getting a blank page using the raw code with my form/field IDs. I see this error in the log files:

    PHP Parse error: syntax error, unexpected ’15’ (T_LNUMBER), expecting ‘)’ in /var/www/vhosts/curlysicecream.com/httpdocs/wp-content/themes/oceanwp-child/functions.php on line 229

    Any thoughts?

    Reply
  13. Jacob
    Jacob September 7, 2017 at 1:58 am

    This is great, but any way where I can set the limits dynamically based on the input of another field?

    For example.. Since we don’t have proper checkbox products in gravity forms.. (yes I saw https://gravitywiz.com/checkbox-products-gravity-forms/, but I don’t think it’s viable when you have a few hundred options).. A person is buying and selecting seats to a concert..

    How many tickets do you want to buy? (Product Dropdown: 1..10).

    Results of the dropdown will limit the number of checkboxes/seats that they can select.

    Let me know if it’s possible either with this snippet or with your plugin at https://gravitywiz.com/documentation/gravity-forms-limit-checkboxes/

    Reply
    1. Jacob
      Jacob September 7, 2017 at 7:08 pm

      Thanks for sharing.. this is good.. I will play with it, I see the configuration section will need to be populated with the ID’s and values..

      But it seems to be related to specific values of a dropdown. What if you want a user to enter in any number (i.e. a quantity)?

      Would this work for (a) spanning mutiple fields (using the Limit Checkboxes Perk) and (b) 2 different limits based for 2 different fields?

    2. Jacob
      Jacob September 7, 2017 at 7:21 pm

      My initial testing on a site using the limit checkboxes perk, using a “Product” in the form of a dropdown doesn’t appear to be impacting.. Does the “Limit how many checkboxes can be checked” need to be enabled? If so, do we put a min/max limit, or leave blank? In the product, I tried “Show Values” using both numbers or strings like Q1, Q2, Q3, etc.. neither seemed to have an impact.. Any further documentation?

    3. David Smith
      David Smith Staff September 7, 2017 at 9:56 pm

      Hi Jacob, reach out to us via the support form and we’ll be happy to take a closer look at your setup. Please include an export of your form, your configuration of the conditional limits snippet and the field labels & IDs you are working with.

  14. Tim
    Tim January 19, 2017 at 12:03 am

    Hi, I just stumbled onto your code and it works awesome except I have one dilemma. I have multiple field IDs being used in the same form. I added the snippet for each field ID however I need to restrict the form to max out at 15 selected checkboxes for the entire form with the combined field IDs. Is there a way to make it add together what’s taken from a separate field to combine in total for the form?

    Reply
  15. Greg Percifield
    Greg Percifield November 7, 2016 at 10:25 pm

    The last update for this reads, “Updated to only apply validation on currently submitted page for multi-page forms.”

    What would I need to do if I wanted this to validate from any page?

    Reply
    1. David Smith
      David Smith Staff November 8, 2016 at 9:12 am

      Hey Greg, I’m not sure you’d ever want this. If it validates from every page, you would get validation errors on form pages that are inaccessible.

      I would also recommend using the perk-version of this snippet which has been dramatically optimized.

  16. Marcus
    Marcus August 10, 2016 at 11:20 am

    THANK YOU for this great snippet & the introduction to Gravity Perks! For the users above who are experiencing the ‘white screen’ after copying & pasting the snippet into their functions.php, it COULD be that they are just copying one of the snippets shown on this page, such as:

    new GFLimitCheckboxes(115, array( 5 => array( ‘min’ => 2, ‘max’ => 3 ) ));

    RATHER than using the FULL CODE from the “DOWNLOAD CODE” button at the top of the page. The instructions within “How do I install this snippet?” do make it sound as if they only need to copy the small snippet rather than the more extensive code available within the “DOWNLOAD CODE” button.

    Hope this helps someone!

    Reply
  17. Jamil Ahmed
    Jamil Ahmed July 15, 2016 at 7:23 am

    Hi David,

    Really a great snippet and very useful. Few days ago I did it with javascript on front-end but this method is quite elegant. Just thinking about drop-down or multi select fields is that possible to customize this snippet to limit selection in dropdown or multiselect fields?

    Reply
    1. David Smith
      David Smith Staff July 15, 2016 at 8:56 am

      Hi Jamil, glad you found this snippet useful. Drop downs only allow one selection regardless. It is possible to limit multiselect fields but the code would require many changes.

  18. Codep
    Codep June 28, 2016 at 9:16 pm

    Dear David, Is it possible to apply this to post categories, or product categories in the Woocommerce form? Thank you.

    Reply
    1. Codep
      Codep July 3, 2016 at 2:32 am

      Thanks David! The trouble with all those solutions is that they operate in the browser, which the user can change.

  19. Sandi
    Sandi May 29, 2016 at 8:27 pm

    I’m interested in having 15 questions, each a checkbox with one option.

    Do you like scarves? as the question and a single checkbox “yes” underneath. I’ll have 15 such questions but I only want people to be able to select 8 checkboxes, i.e. select 8 yes times. Is this possible since the checkboxes are in different questions? If so, how? And do I need to buy the plugin to achieve this?

    Reply
  20. Daniel Crowe
    Daniel Crowe May 26, 2016 at 1:08 am

    Great little snippet, will come in handy.

    Any chance you can help me with something? I have two separate checkbox fields, can we combine them so say no more than 7 can be checked between the two checkbox fields? Get what im saying?

    Thanks -Dan

    Reply
  21. J
    J April 14, 2016 at 5:50 am

    I’ve installed the snippet to my theme’s functions, got the white screen so I did the troubleshoot and decided to just remove everything altogether but I’m still getting the white screen. Any idea on how I can get my site back? Thanks so much.

    Reply
    1. David Smith
      David Smith Staff April 14, 2016 at 9:14 am

      Hi J, if the site is still white screened there are three likely issues:

      1. All of the code has not be removed.
      2. All of the code has been removed and you accidentally removed a little of your functions.php’s original code.
      3. Something else is causing an issue.
  22. Irene
    Irene March 21, 2016 at 5:59 pm

    The snippet is not working on my end. I’ve copy and pasted the snippet into the theme’s functions.php file and configure the class at the bottom but returns an empty page.

    Reply
    1. Irene
      Irene March 25, 2016 at 7:04 pm

      I’ve followed how the snippet should be pasted in the functions.php. Also, I’ve modified the class below according to the form and field ID I am using and it makes the page empty.

  23. Jessica
    Jessica March 4, 2016 at 5:13 pm

    Thank you so much!!! I am so glad I found this. I am at the point where I can do advanced customizations, but I don’t write code. I am slowly teaching myself by patching in other code like this.

    Reply
  24. Lyse
    Lyse February 19, 2016 at 1:53 pm

    Hi David,

    This looks like a very useful snippet. I added the code to my functions.php file.

    When you talk about the class format this snippet uses, where does one make a reference to the class, is it in the Gravity form field that users the checkboxes? If so, I’m not sure which I should use. The form I’m creating is intended to be used with WooCommerce Gravity Form Addon, so to add additional fields and data when making a purchase.

    I tried the different class format by copying and pasting your examples, into one of my checkbox field I want to have limitations. When I preview my form I don’t see any change. Is is possible I need to have my form on the live website to see the results?

    Any clarification on how to use the class in my forms would be greatly appreciated.

    Lyse

    Reply
    1. David Smith
      David Smith Staff March 8, 2016 at 10:40 pm

      Hi Lyse, I’m not sure I understand your question. The basic premise is that you copy and paste the snippet into your theme’s functions.php file and then configure the snippet (down at the bottom) to match your form and fields as needed.

    2. Lyse
      Lyse March 9, 2016 at 8:37 am

      Hi David,

      I realized, after writing my post, that I need to purchase the Perks bundle for this snippet to work. I’m not quite ready to justify purchasing the Perks bundle at this time.

      Thank you for responding though. Lyse

    3. David Smith
      David Smith Staff March 9, 2016 at 7:04 pm

      Hey Lyse, this snippet does not require you to purchase the Perks bundle. It works by itself. The perk-version of this functionality provides a UI for enabling it and also a couple additional features.

  25. Ruben
    Ruben February 15, 2016 at 6:30 am

    Hi David, I copy-paste, without the <?php in functions.php file. Error 500… Could you give me help? i don’t understand why..

    Reply
  26. Andrei G
    Andrei G January 19, 2016 at 11:05 am

    Hi David,

    In my case, I am looking to limit the number of checkboxes selected across multiple fields. I have a form that allows users to order product samples and the checkboxes are split in multiple fields, by brand, for easier grouping and selection.

    Is this possible to do with this code?

    Thank you.

    Reply
  27. Jennifer
    Jennifer September 28, 2015 at 12:48 am

    Hi,

    I’m wondering if there is a way to set the values for MIN_NUMBER and MAX_NUMBER based on the quantity entered in a product field (ie, #ginput_quantity_2_28)?

    I’m using your List field choices snippet (https://gravitywiz.com/use-list-field-choices-gravity-forms/) to populate a set of checkboxes. Now I would like to limit how many of those checkboxes can be selected according to the quantity entered in my product field.

    I just don’t have the skills to figure out how to extract that quantity value and use it as my min/max when I call GFLimitCheckboxes.

    Your help would be much appreciated! BTW, please let me know if that’s something that the Gravity Perks plugin can do as I would be happy to purchase that instead of asking for free help. :)

    Reply
    1. David Smith
      David Smith Staff September 28, 2015 at 8:21 am

      Hi Jennifer, I took a look over the code and there doesn’t appear to be a simple way to accomplish this. It’s a cool idea though. Would you be interested in commissioning me to add this feature? If so, get in touch.

    2. Jennifer
      Jennifer October 1, 2015 at 8:03 pm

      Hi David, Thanks for reviewing the code for this! I’m exploring a few form options to build what we need but I’ll definitely get in touch if we decide this feature is a must have. Thanks, Jennifer

  28. George
    George August 10, 2015 at 2:54 pm

    Hi David,

    First, thank you for a great snippet of code!

    I’m having an issue when I use the snippet in my functions.php. When I go to save the page where the Gravity Forms shortcode is on, now I’m getting this to appear on a blank page:

    Warning: Cannot modify header information – headers already sent by (output started at /home/emsco05/public_html/dev/vrdev/wp-content/themes/mobilefirst/functions.php:604) in /home/emsco05/public_html/dev/vrdev/wp-admin/post.php on line 237

    Warning: Cannot modify header information – headers already sent by (output started at /home/emsco05/public_html/dev/vrdev/wp-content/themes/mobilefirst/functions.php:604) in /home/emsco05/public_html/dev/vrdev/wp-includes/pluggable.php on line 1196

    line 604 starts as: <?php endif;

        return $gform;
    }
    

    It’s correlates to your php code.

    Any insight as to why?

    Thank you! George

    Reply
    1. David Smith
      David Smith Staff August 20, 2015 at 8:39 am

      Are you using a page builder plugin of any kind? Many of these fire the pre render hook but fail to capture some custom scripts output with the form block which would cause this kind of an error.

    2. George
      George August 20, 2015 at 11:04 am

      Sorry, to answer your question, I’m not using a pagebuilder plugin. However, I do have WooCommerce. The theme I’m using is a Genesis simple child theme that I have heavily customized.

    3. David Smith
      David Smith Staff August 20, 2015 at 11:28 am

      Yeah, it sounds like that plugin is conflicting. I’m not able to debug plugin conflicts like this pro bono. If you’re interested in hiring me, get in touch.

  29. Lucien P.
    Lucien P. June 18, 2015 at 3:53 am

    Amazing, just copy/paste in functions.php, use your parameters in GFLimitCheckboxes, and perhaps translate the validation messages, and it works like a spell ! If you only have a minimum constraint however, you need to set the maximum parameter to a very big number, in order to avoid the “undefined number” condition. Thank you for sharing !

    Reply
    1. David Smith
      David Smith Staff June 18, 2015 at 6:52 pm

      Glad you like it! The only easier way to install and use this functionality (plus the bonus feature of being able to share the limit across multiple checkbox fields) is the perk-version: GP Limit Checkboxes.

  30. Rob G
    Rob G January 29, 2015 at 10:00 am

    Does this snippet work with GF 1.9 that was just released (1/28/15)? Just tried it in functions.php and it doesn’t seem to do anything.

    Thanks,

    Rob

    Reply
    1. Rob G
      Rob G January 29, 2015 at 10:23 am

      Actually, sorry — it does work! PHP’s opcache wasn’t invalidating functions.php on the server and picking up the new changes. :)

      Thanks for your work.

      Rob

  31. Susan Velez
    Susan Velez November 29, 2014 at 10:21 am

    I was browsing the internet to find out how to limit the number of checkboxes with Gravity Forms. This code is awesome and works flawlessly. Thanks for sharing it and I hope you have a great day.

    Susan

    Reply
  32. Asaf
    Asaf October 21, 2014 at 3:05 am

    Hi,

    I’m using your code along with Gravity Forms Addon plugin for woocomerce.

    I have a product with variations, that once a variation is selected the gf form appears and lets me choose product features. Is there any way to set different limitations to same gf field based on the variation selected?

    Thanks

    Reply
    1. David Smith
      David Smith Staff October 21, 2014 at 9:27 am

      Hi Asaf, this is not possible without additional custom code. It is possible immediately if you didn’t need it to apply to the same field.

    2. Asaf
      Asaf October 26, 2014 at 6:32 am

      It’s not really what I need. I need some kind of condition in your code that will have 2 cases: 1. Variation A => limit selection for 2 choices 2. Variation B => limit selection for 4 choices

      The example you gave me deals with weather to show/not show selections

    3. David Smith
      David Smith Staff October 26, 2014 at 7:41 am

      Ah, yes, I confused myself. There really is no simple code example I can provide that demonstrates how to accomplish this. Are you unable to use multiple fields with different checkbox limits and show/hide the appropriate field based on conditional logic?

    4. Asaf
      Asaf October 26, 2014 at 7:47 am

      LoL :)

      I’d rather use same fields with limitation conditions, however if this can’t be done, how can I create a conditional logic that shows filed 1 for woocommerce variation A, and filed 2 for woocommerce variation B?

      GF conditional logic are based on GF fields only, right? There’s a way around it – using GF product flied, but I want to use WOO product options.

    5. David Smith
      David Smith Staff October 26, 2014 at 8:06 am

      Ah, I did not realize the variations were on the WC product. Assumed you were talking about a Gravity Forms product + options. So this would required two levels of customization:

      1. Adding the ability to determine which WC product variation is selected.
      2. Assigning a checkbox limit (and reinitializing the limit script) whenever a variation is selected.

      Typically, I would love to tackle a small customization like this but I’m a little booked up at the moment. Give Codeable.io a try. They specialize in small customizations.

    6. Brandon
      Brandon December 15, 2014 at 2:35 pm

      Asaf, I’ve run into the exact same situation that you discussed with David. I’m curious if you had any luck getting this figured out.

      Thanks so much for taking the time to respond!

      ~ Brandon

  33. Aaron
    Aaron June 25, 2014 at 9:37 pm

    This is great! Thanks for your help.

    I personally would suggest making the checkbox feel more like a radio button, as that’s what most users , at least in my mind and personally, would interact with the checkboxes if they must be selected at a max of ‘1’.

    I understand it’s probably a better idea to use Radio Buttons if that’s the case on my end, but my project specifically requires a checkbox. This of course is a hack to a hack, so… I can’t ask for too much, eh?

    Thanks again.

    Reply
    1. David Smith
      David Smith Staff June 27, 2014 at 5:38 pm

      Hi Aaron, thanks for the feedback. Using a checkbox field with a max of “1” is a strange requirement. Can you share more details on the reasoning?

    2. Aaron
      Aaron June 29, 2014 at 12:06 am

      Specifically David, The radio button option for PayPal Payments Pro auto-calculates and de-calculates based on user interaction.

      I agree with my client that it is a bit confusing, therefore I specifically chose to use Checkboxes.

  34. Tony
    Tony May 21, 2014 at 1:11 pm

    I was curious if there is additional code that could be added to this to limit check-boxes across multiple fields? As an example, If i had 3 separate check-box fields, “vegetables, pastas & risottos” You could only choose 1 option from all three fields. Any help is appreciated.

    Reply
  35. Bob
    Bob May 20, 2014 at 9:01 am

    Hi David,

    First, thanks for all the snippets – some very useful looking stuff that I hope to use in the future.

    Sorry if these are stupid questions (and no reason at all for you to reply) but:

    1) Could this be easily customized to check for the number of checked selections in more than 1 select box? 2) Could the limit be set either by selecting a product quantity field, or passed as a variable?

    Thanks either way, as I said I really appreciate what you’ve posted here and elsewhere.

    Cheers

    Bob

    Reply
    1. David Smith
      David Smith Staff May 20, 2014 at 9:38 am

      Hey Bob, the GP Limit Checkboxes perk (based on this snippet) provides the ability to span the limit across multiple checkbox fields.

      Setting the limit via a product quantity field or passed as a variable is possible with some custom work. Get in touch with me if you’d like to discuss this further. :)

    1. David Smith
      David Smith Staff March 27, 2014 at 4:09 pm

      Thanks for the praise, Justin! You might be interested to know this functionality (with a per field UI and other enhancements) is available with Gravity Perks. Even easier to install and configure and includes automatic upgrades to always ensure compatibility with the latest version of Gravity Forms.

  36. Tyler
    Tyler March 26, 2014 at 5:06 pm

    I’m late to the game but for anyone like me (knows enough to be dangerous and crash sites) if you add this to your funtions.php and it dose not work or crashes just at the ?> before the starting <?php of this code.

    Reply
  37. Eve
    Eve March 12, 2014 at 7:42 am

    Hi, I’m using this code (GFLimitCheckboxes) for long time – it’s great! But now I need limit checkboxes for specyfic post ID. Could You help me? Best regards! Eve

    Reply
    1. David Smith
      David Smith Staff March 12, 2014 at 9:24 am

      Hi Eve, to clarify, do you mean that you want to change the limit on a specific form depending on which post it is embedded in?

    2. eve
      eve September 29, 2014 at 10:13 am

      Exactly. I have: new GFLimitCheckboxes(23, array( 77 => array( ‘min’ => 1, ‘max’ => 3 ), 85 => array( ‘min’ => 1, ‘max’ => 3 ) ));

      and for specyfic post ID I need change ‘max’ parameter from 3 to 5. But only for this one post.

      Best regards!

  38. Tim
    Tim January 16, 2014 at 6:06 pm

    David, this is the first time I have used Gravity forms and your snippet has just made it absolutely fit for purpose for my clients needs. Many thanks for taking the time to explain the process out. Tim

    Reply
  39. Kris
    Kris November 19, 2013 at 1:57 pm

    Thank you for this code, it was exactly what I was looking for. Is there an easy way to make the word selections more grayed out after the limit of checkboxes has been reached instead of just disabling the checkboxes?

    Thanks again, Kris Rhoades

    Reply
    1. David Smith
      David Smith Staff November 21, 2013 at 10:11 am

      Hi Chris, this is possible with a little CSS: input[type="checkbox"]:disabled + label { color: #999; }. Just paste that in your theme’s stylesheet. More information available here.

  40. Sam
    Sam November 15, 2013 at 5:56 pm

    Hey David,

    Thanks so much for the fantastic snippet! It works great on the current form I’m working on, but I noticed a jQuery conflict in my situation.

    My form is multi-page and utilizes Ajax, and when the Next page button is pressed, a “jQuery is not defined” error is logged. I’m guessing the (document).ready aspect is being called multiple times, but everything seems to be working regardless.

    Not sure if it’s a huge issue, but any ideas how to get around this? Thanks in advance

    Reply
  41. susan jones
    susan jones November 13, 2013 at 12:49 am

    I’m not sure what to do after I copy the code specified into my theme’s function.php. Then what? I’m obviously missing something. I’d really like to get this to work.

    Thanks!

    Reply
    1. David Smith
      David Smith Staff November 13, 2013 at 10:46 pm

      Hi Susan, check out the “Do I need to modify this snippet to work with my form?” section in the article above. It provides some examples of how this snippet can be initialized to work with your form. Let me know if you have any questions based on those examples.

  42. Ciaran Whelan
    Ciaran Whelan September 1, 2013 at 9:28 pm

    Hi there, this looks like an awesome snippet even when I have tried your demo. However it seems very close to what I want to achieve.

    You have asked for a description or scenario where we need more than one filed to be triggered with this.

    Scenario:

    We are requiring some feedback from students on a scale of 1-6 on usability of 6 personas for the survey.

    I have created 6 sets of number fields labeled for different personas. I have limited the number field of a range 1-6.

    However I want that the user can only choose 1 number for the ranking of the 6 personas, and not use a number that has been chosen.

    persona 1 : rank order of importance 1-6 persona 2 : rank order of importance 1-6 persona 3 : rank order of importance 1-6 persona 4 : rank order of importance 1-6 persona 5 : rank order of importance 1-6 persona 6 : rank order of importance 1-6

    If they choose persona 1 has importance of 2, then the number 2 cannot be used for the other 5 personas in this ranking survey form.

    Hopefully this explains the scenario of what I would like to achieve.

    Regards,

    Ciaran

    Reply
  43. Brittany
    Brittany July 12, 2013 at 3:03 am

    I copied & pasted as-is, and I’m getting this error:

    Parse error: syntax error, unexpected ‘”‘ in themes/Avada/functions.php on line 788

    the code on that line is: $script .= “jQuery(“” . implode(‘, ‘, $selectors) . “”).checkboxLimit({$max});”;

    Any ideas? Thanks.

    Reply
  44. Keely
    Keely June 10, 2013 at 8:54 pm

    Just thought I’d let you know I can see formatting in my email notifications – ie …

    something here

    Cheers

    Reply
    1. Keely
      Keely June 10, 2013 at 8:55 pm

      Formatting didn’t show in my post – around something else there should be opening and closing paragraph tags. So the tags are showing in my email. No big deal – just thought you may like to know?

    2. David Smith
      David Smith Staff June 11, 2013 at 11:35 am

      Hey thanks for letting me know Keely. I think this is how the comment notification emails work by default with WordPress? Let me know if you know otherwise.

  45. Brian
    Brian June 10, 2013 at 4:05 pm

    David,

    Sorry to bother you, what may help some people is letting them know that they need to add the script in the page template in order for it to work. Seems that as soon as I looked at the page source for your demo, I noticed the script there, copied it and pasted it in my page template and it worked! Now I’m going to play around with multi-forms and see how this works. I’ll let you know if I figure it out!

    Thanks,

    Reply
    1. David Smith
      David Smith Staff June 10, 2013 at 8:54 pm

      Hi Brian, you shouldn’t have to paste anything into the actual page template. If you can setup the issue you were having (prior to manually pasting the

  46. Brian
    Brian June 10, 2013 at 4:01 pm

    Hey David,

    Can’t seem to get this to work either. Added the php to functions.php at the bottom (removed the <?php from the top), added my form id, field id, and then my min/max. No dice. Tried in other browsers as well.

    Also, the code displayed for multiple checkboxes in a form and multiple forms is exactly the same. Seems the code is basically geared towards multiple fields and not forms. Can you update this?

    Thanks!

    Reply
  47. Brian
    Brian May 24, 2013 at 11:10 am

    David, Another great code snippet! Is there any way we could modify the code to allow these selections to be made in the settings or somewhere on the form, instead of having to edit the code snippet every time?

    Reply
    1. David Smith
      David Smith Staff May 15, 2013 at 8:54 am

      Hi Ng Mun Soon, this snippet can be configured with no minimum which would allow you to submit the form with less than the maximum checked. This was how the demo was configured. I’ve updated the demo to now include a minimum as well so you can see how the validation works. :)

  48. hannan
    hannan May 7, 2013 at 6:01 am

    hi can anybody help me with this issue? i must tell you that i am a newbie

    As the post says that it limits the number of checkboxes. i have copy and pasted the above mentioned code to functions.php but when i update the function, it comes up with blank white screen. more over my website is not opening. somehow i removed this issue but main problem of limitizing the number of checkboxes still persists.

    kindly help me by defining from which line of code i should copy and past in function.php also kindly tell me at what place to paste it.

    it would be helpful if you kindly identify the line of codes

    Reply
    1. David Smith
      David Smith Staff May 7, 2013 at 8:14 am

      Hi Hannan,

      More than likely you simple do not need to include the opening <?php tag from the snippet. Try copying and pasting the snippet from here. Be sure to leave the opening <?php tag out.

  49. Keely
    Keely April 14, 2013 at 2:01 am

    This actually won’t work if you have a set of products as radio buttons. I’m starting to tear my hair out trying to figure out how to do my client’s forms using GF but client doesn’t want a shopping cart, although a shopping cart would work much better. Note to self – no more client websites – going to make WordPress themes.

    Reply
    1. David Smith
      David Smith Staff April 16, 2013 at 10:01 pm

      Hi Keely, you are correct. This is only available for fields with the Checkbox input type.

  50. Keely
    Keely March 18, 2013 at 3:41 am

    Sorry David !

    I must be too impatient … I actually tried using the github php file yesterday but that didn’t work either.

    The raw version didn’t throw any errors and worked … chose 1 item and submitted and got “You must select at least 2 items.”

    I then selected 2 items then got “You may only select items.”

    This is the code I have at the bottom

    new GFLimitCheckboxes(1, array( 40 => array( ‘min’ => 2 ) ));

    So I put a max value in and now it’s this

    new GFLimitCheckboxes(1, array( 40 => array( ‘min’ => 2, ‘max’ => 8 ) ));

    That worked :) – there are 8 checkboxes in the field.

    So it needs a max value. Just letting you know.

    Thanks for pointing me to the file – much appreciated.

    Reply
  51. Keely
    Keely March 17, 2013 at 7:22 am

    Hi – I’m getting an error as well …

    Parse error: syntax error, unexpected ‘”‘ in functions.php on line 51 – which is line 48 in your snippet.

    Would love to get this to work as I need to force 2 or more checkboxes on a field.

    Thanks

    Reply
    1. Keely
      Keely March 18, 2013 at 2:49 am

      I left a comment here and it has disappeared … I posted that I’m getting an error related to line 48.

      Getting a developer to fix it.

    2. David Smith
      David Smith Staff March 18, 2013 at 2:53 am

      Hi Keely, not disappeared, just hadn’t been approved yet. I like to make sure I answer each comment and waiting to approve them until I answer has been my only method for now. :)

      With that said, if you copy and paste this snippet form the raw version I’m betting it will work. Give it a shot and let me know.

  52. Courtney M
    Courtney M February 8, 2013 at 2:10 pm

    I am running into some conflicts when implementing this on a multi-page form.

    (1) I get this error at the top of the form:

    Warning: array_key_exists() [function.array-key-exists]: The first argument should be either a string or an integer in /home/content/a/q/u/aquaten/html/wp-content/themes/AWDMarch2011/functions.php on line 772

    Line 772 is the first line in the snippet below:

            if( !array_key_exists($field['id'], $this->field_limits)    // if field limits not provided for this field
                || RGFormsModel::get_input_type($field) != 'checkbox'   // or if this field is not a checkbox
                || !isset($this->field_limits[$field_id]['max'])         // or if 'max' is not set for this field
                )
                continue;
    

    (2) When I click the button at the bottom of any page to continue to the next page in the form I get a validation error. There are no errors on the page but it will not let me continue until I “fix the errors.” If I remove your script, it lets me continue without any error. Seems to be a JQuery conflict.

    Here is a link to the form (the checkboxes in question appear on the last page of the form): http://www.evolutionswim.com/test

    Reply
    1. David Smith
      David Smith Staff February 13, 2013 at 7:44 am

      Hi Courtney, the snippet has been updated to only validate checkbox fields on the currently submitted page. This resolves your issue #2. I’m unable to recreate your first issue. I’ve sent you an email requesting more details.

  53. James Maabadi
    James Maabadi January 18, 2013 at 10:22 pm

    Hello, Thanks for this great snippet! But I have something weird going on, when I provide the correct form id and field id I get the following error: Fatal error: Call to undefined method GFFormDisplay::add_init_script() on line 44

    I have your snippet in it’s own file, then including it in my functions file.

    Reply
  54. Adam
    Adam January 10, 2013 at 1:23 pm

    So just wanted to check to see if this is still working, I just tried it a few different ways adding it to my functions.php, then had to close the php tag to get the page to load. It still doesn’t work. I have added it to twenty twelve as well and that doesn’t work either so shouldn’t be a theme conflict. My form id is 5 and the check box field id is 5 field after 5 is 6 so I used this new GFLimitCheckboxes(5, array( 5 => array( ‘min’ => 2, ‘max’ => 3 ), 6 => array( ‘max’ => 3 ) ));

    I just copy and pasted the top section into functions.php and changed the above section to match the 5 and 6 ids.

    Reply
    1. Adam
      Adam January 10, 2013 at 2:07 pm

      Never mind got it working, must have been a caching issue with firefox as I checked with chrome and IE and it worked. I was clearing cache with firefox as well but didn’t work till I closed all instances of firefox. Thanks for this.

  55. FanaticWeb
    FanaticWeb December 16, 2012 at 9:22 pm

    Hi David, VERY useful snippet first of all, really wished it can be integrated with GF directly or make a plugin out of it.

    I’m trying to apply the limitations on Multi-Select and Drop down menus, I tried your code and got an error:

    Warning: array_key_exists() [function.array-key-exists]: The first argument should be either a string or an integer in /homepages/xx/dxxxxx/htdocs/site/wp-content/themes/thetheme/functions.php on line 294

    My guess is the snippet will only work on Checkboxes as the title indicates? Is there a way to apply it on other selections such as the Drop down menus, multi-select, etc. (excluding Radio buttons of course)

    Nevertheless, great work!

    Reply
    1. David Smith
      David Smith Staff December 17, 2012 at 7:28 am

      Hi FanaticWeb, this snippet will actually be a perk soon enough. :)

      This wouldn’t really apply to single drop down feilds; however, I can definitely see the benefit with multi-selects. This isn’t functionality I’m intending to support for this snippet at the time but feel free to hire me if you’d like to see this snippet enhanced and shared on Gravity Wiz. :)

  56. Chris
    Chris December 15, 2012 at 2:06 am

    How can I get this class to ignore fields that are hidden by conditional logic and aren’t necessary unless a particular value is chosen in a previous field? Like when I choose a radio button in field A I’m then presented with checkbox field B. I check the “min” amount of choices in field B and submit my form which also validates hidden checkbox fields C, D, & E, thus causing an error.

    Reply
    1. David Smith
      David Smith Staff December 15, 2012 at 9:38 am

      Hi Chris, I’ve just updated this snippet to support conditional logic. Let me know if you run into any issues with it.

    1. David Smith
      David Smith Staff December 2, 2012 at 6:13 pm

      Hi Michelle, what seems to be your issue? Also, if you can provide a link to your form where this is implemented, I can check for surface-level issues. :)

  57. David
    David November 25, 2012 at 1:17 pm

    Hi Dave

    Thanks for the fast response. I personally need this for my online shop. The visitor can compose a gift basket. There are many different types of products in different fields. He can choose (depending on the package he bought) for example 20 different products. So it would be really cool if we could limit the checkboxes to multiple fields in a form and not just one field. I’m not sure if this is possible, it could be very difficult to write the code I think.

    Cheers,

    Dave

    Reply
    1. David Smith
      David Smith Staff November 25, 2012 at 8:51 am

      I’m seeing this request pop up a bit. Could you (or any others reading this) provide an example form demonstrating a scenario where you would only want a set number of checkboxes checked across the entire form?

  58. Chris
    Chris November 12, 2012 at 7:30 pm

    Hi thanks for the snippet. Like Chris said, It would be really cool if this feature could be over more than one field. That would be great.

    Best regards,

    Chris

    Reply
    1. David Smith
      David Smith Staff November 13, 2012 at 7:13 pm

      Thanks for seconding this request. If it continues to be a popular request, I will definitely consider adding support for it.

    1. David Smith
      David Smith Staff November 4, 2012 at 9:04 am

      To make this functionality work collectively would require significant customization to this snippet.

  59. Chris
    Chris November 3, 2012 at 1:05 pm

    Ahh ok. So one couldn’t pass the parameters to all of the fields in one array to work collectively using a modified method?

    Reply
  60. Chris
    Chris November 3, 2012 at 12:50 am

    Hi David,

    Is there anyway the array can apply to a series of fields together as a whole instead of one by one?

    For example if I have 10 checkbox fields with 4 choices in each out of the 40 checkboxes I only want the user to be able to check 1 box out of them all.

    Would this work for that?

    Reply
    1. David Smith
      David Smith Staff November 3, 2012 at 2:00 am

      This functionality only applies to limiting checkboxes within an individual field. It can be applied to multiple fields but the limit will only apply to each field independently.

  61. Chris Stott
    Chris Stott October 31, 2012 at 6:03 pm

    This is fantastic thanks! I’m wondering how I’d make a minimum number of selections as well? So instead of ‘pick up to 3’ it would be ‘pick exactly 3’ – no more, no less. Maybe an idea for a future snippet!

    Reply
    1. David Smith
      David Smith Staff November 3, 2012 at 12:06 am

      Hi Chris, this snippet has just been updated to support setting a minimum number of checkboxes which must be checked. The article has been updated to provide additional instruction for using the updated snippet.

  62. Jeremy
    Jeremy July 26, 2012 at 8:35 am

    If you wanted to apply this to more than 1 form, could you put the gform_pre_render_115 in an array as well? Or would you just use add_filter('gform_pre_render_115,gform_pre_render_116','gwiz_limit_checkboxes'))

    Reply
    1. David Smith
      David Smith Staff July 26, 2012 at 11:10 pm

      Hi Jeremy,

      I’m going to be adopting a new method for most of my snippets (as required) that allows you to reuse the snippet code on multiple forms more readily. Check out the updated snippet and instructions above and let me know if you have questions. One note for you. You can apply the new snippet to multiple forms like so:

      new GFLimitCheckboxes(115, array(5 => 3));> new GFLimitCheckboxes(89, array(1 => 2));>

    1. David Smith
      David Smith Staff June 12, 2012 at 1:47 pm

      Could you provide a link to a form where you’ve implemented this? I will setup a demo form with this working now.

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Download Snippet

Limiting How Many Checkboxes Can Be Checked

This field is for validation purposes and should be left unchanged.