CMS Development

scottywc
Member

Limit number of responses on checkbox form

I have a form that have a section of check boxes, with 8 options. I want to limit the number of selections to only 3. Is there a way to do this? 

4 Replies 4
GrantCarlile
Top Contributor | Diamond Partner
Top Contributor | Diamond Partner

Limit number of responses on checkbox form

Who has found success limiting the number of checkbox options selected? Please share with us how. Thank you! GC

0 Upvotes
ChadP
Contributor | Elite Partner
Contributor | Elite Partner

Limit number of responses on checkbox form

Try this. It Check to see if the siblings of the current checkbox are checked and it they are and there are more than 3 then it won't allow any more.

 

Make sure and replace 'ID OF CHECK BOX HERE' with the correct input ID or name.

 

var limit = 3;
$('ID OF CHECK BOX HERE').on('change', function(evt) { if($(this).siblings(':checked').length >= limit) { this.checked = false; } });

 

marc-antoine
Participant

Limit number of responses on checkbox form

Thanks Chad,
Is it possible that it does not work in my form because the checkboxes are embedded in <ul><li>?
It seems HubSpot does it by default.
https://offres.bureauetbureau.com/test-consultation-video
I am trying to limit the number of checkbox selected for the second question.

Here is the JS I used in the module.

var limit = 2;
$('input[name="style_prefere"]').on('change', function(evt) {
if($(this).siblings(':checked').length >= limit) {
this.checked = false;
}
});

 

Also, there is javascript within the html+HubL section, could that affect this? I entered the JS into the module.js section.
Capture d’écran 2020-03-19 à 04.03.25.png

0 Upvotes
HargerHoweAdv
Member

Limit number of responses on checkbox form

Do you put this in the code for the form itself when you embed? If so, where?

0 Upvotes