Skip Navigation

[Resolved] Limit number of checkboxes allowed

This support ticket is created 5 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by Lykke Scavenius 5 years, 5 months ago.

Assisted by: Minesh.

Author
Posts
#1287315
limit-number-checkboxes-form.PNG
limit-number-checkboxes.PNG

I've researched on this and found some scripts to limit number of selections, but they affect all checkboxes and I have multiple groups of checkboxes.

It's an EDIT post type form, and the group is created from a taxonomy. The psychologists should be allowed to pick only 3 from this groupe, but the other checkbox groups should be unaffected.

Thanks for your time,
Lykke

#1287371

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You can use the Forms's hook: cred_form_validate

Please check the following example - that is to count at least one checkbox but you can adjust the code to threee checkboxes:
=> https://toolset.com/documentation/adding-custom-code/require-at-least-one-checkbox-of-a-checkboxes-field-to-be-checked-in-a-toolset-form/

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

#1288517

Hi 🙂

I tried inserting this code in toolset settings, but there are problems

1. I don't know what the "field" is, since this checkbox list is generated from a taxonomy, it doesn't work with the taxonomy slug, so what field is it, is it the title field of the taxonomy? And what's that called, it's the standard taxonomy title field.

2. How do I customize this code to prevent more than 3, instead of this that required a minimum.

Also, I was looking at this method hidden link it looks simple and it just prevents from clicking more than 3, but the issue with this, is that it - as far as I can see, would affect every form.

One last thing, is there a way to add "info/messages" in the form, like small notes to help with the fields?

Thanks for your time
Lykke

#1288533

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The JS reference example link you shared is for frontend validation.

Can you please share problem URL and access details?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1289583

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've added the following code to your form's JS box:

jQuery(document).ready(function ($) {
var tema_limit = 3;
  
$('input:checkbox[name="tema[]"]').on('change', function(evt) {
    if($('[name="tema[]"]:checked').length >= tema_limit) {
       this.checked = false;
   }
});
});

And with tema checkboxes field now you can select only 3 checkboxes.

#1292053

My issue is resolved now. Thank you!