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!
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!
input[type="checkbox"]:disabled + label { color: #999; }
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 46+ essential add-ons for Gravity Forms with support you can count on.
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.
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.
Is it possible to limit the options of a selector with populate anything?
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,
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.
Hi Carlos,
We have a Perk version of this functionality, GP Limit Checkboxes Perk, that should work when using GP Populate Anything. Please check it out.
Best,
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!
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,
Samuel!
this module: https://docs.gravityforms.com/multi-select/
Hi Carlos,
The Snippet and Perk don’t work on a Multi-Select field. We do, however, have a Javascript snippet you can use to limit selection on a Multi-Select Field.
https://github.com/gravitywiz/snippet-library/blob/master/gravity-forms/gw-limit-multiselect.js
Check out the snippet page for more details on how to use it and give it a try.
I hope this helps.
Best,
Samuel!!!! I solved it!!!
http://snippi.com/s/faxzsjg
Hi Carlos,
Glad you were also able to find a solution that works for you.
Best,
Samuel! It is possible that the snippet does not work in mobile?
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,
Hi Carlos,
The snippet has been updated and it works now on mobile devices.
Best,
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 ) ));
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,
Just wondering where the form ID and field ID need to be put in the code? The image attachments to git hub dont work.
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.
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 ) ));
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,
Hi Samuel
It works! I was using a code plugin and forgot to activate the snippet!
Thanks for the help.
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!
This appears to have resolved itself – I think we were overcaching our JS. Thanks anyway!
Hi Mel,
Yes, this is often a caching-related issue. Glad you were able to get it working.
Best,
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
nvm it work now
Hi Razi
Glad you were able to figure it out.
Cheers,
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.
Hi Haya,
We have an experimental snippet that you can use to limit a Multi-Select field.
You can use our free GF Custom Javascript plugin to add the javascript snippet to the form.
I hope this helps.
Best,
Any way to take the markings of the entire form and not of independent fields?
Hi Guillermo,
I’m not sure if I’m tracking, but if you want to span the limits across multiple fields, this is supported in the perk version of the plugin.
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!
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,
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?
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,
Unchecking boxes doesn’t appear to be making the other boxes enabled again in GF 2.5.16. Any ideas?
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.
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
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,
This no longer works with Gravity Forms 2.5!
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.
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…
Hi Katie, not in the snippet, but spanning limits across multiple checkbox fields is available in the Perk version.
GP Limit Checkboxes