Limit amount of checkboxes that be checked in Gravity Forms – Javascript

Published:

Limit amount of checkboxes that be checked in Gravity Forms – Javascript

I was looking for a way to limit check boxes, as gravity forms doesn’t current offer that to start.  Then I came across this little gem on JasonPaul.net.  This allows you to select the ID of the check boxs and set a limit to the amount that can be checked!

Just copy and paste, be sure to put in the ID of Unordered List that you have, as well as changing the number to the amount you want to limit your selection to.

Enjoy!

<script type="text/javascript">
$.fn.limit = function(n) {      
     var self = this;      
     this.click(function(){ return (self.filter(":checked").length<=n); });  }     
     $("ul#YOUR-UNORDERED-LIST-ID li input:checkbox").limit(5);
});
</script>

Or you can do this in the with a function. Just add this to the functions.php file!

new checkboxlimit(69, array(
     8 => array(
        'min' => 2,
        'max' => 3
      )
));

Now select which form or forms by ID using this bare bones example

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

Can also do it with multiple forms and fields

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

new checboxlimit(FORM_ID, array(
FIELD_ID => array(
‘min’ => MIN_NUMBER,
‘max’ => MAX_NUMBER
)
));