How to limit the number of selected checkboxes

How can i limit the number of selected checkboxes ?

This can be achieved in two ways: through a Javascript or a PHP method. In this article we will focus on the PHP method.

Since revision 41, you can invalidate a field directly from within the Scripts called on form process area. This allows total control of the validation flow during the submission process. Having this into consideration, we can easily use the following script for the mentioned purpose (placed in the Properties > PHP Scripts: Scripts called on form process):

// Replace NAME-OF-CHECKBOX-GROUP with the actual name (not caption!) of your RSForm! Pro checkbox field.
// We'll assume you want a maximum of 3 selections.
$max = 3;
// Checks if the current submitted values are within the limit.
if (count($_POST['form'][$checkbox]) > $max) {
  // $invalid is an array that contains the IDs of fields that did not pass validations.
  $invalid[] = RSFormProHelper::getComponentId($checkbox);

Instead of the NAME-OF-CHECKBOX-GROUP you will have to use the actual name of your checkbox group. Instead of 3, setup a numeric value - this will be the maximum selections within the checkbox group.

6 persons found this article helpful.

Was this article helpful?

Yes No
Sorry about that