How to Set a Minimum and a Maximum Number of Choices for a Checkbox

Overview

Would you like to set a minimum and a maximum number of choices for your Checkbox form field? Inside the form builder, you can already set a maximum number for this field. However, using a little PHP snippet did you know you can also force a minimum amount as well? In this tutorial, we’ll show you the PHP code snippet needed to complete this.

Setup

You can already set up your forms to set a maximum number of choices for your Checkbox field by following the steps in this tutorial.

1) Setting up the form

First, set up your form to add a Checkbox field and set the maximum number to match your needs. In our example, we’ve set our Choice Limit to three so only up to three selections can be chosen.

Set the maximum number of choices visitors can choose on your checkbox

2) Adding the PHP

Next, you’ll need to copy the code snippet below and add it to your site. In our code, we’re going to target the form ID of 787 and the field ID of the Checkbox form field of 11. This code is checking to make sure at least two items have been selected from the list.

/**
 * Set minimum and maximum number of choices on Checkbox
 *
 * @link https://wpforms.com/developers/how-to-set-a-minimum-and-a-maximum-number-of-choices-for-a-checkbox/
 *
 */
function wpf_checkbox_validation( $field_id, $field_submit, $form_data ) {

	$field_submit  = (array) $field_submit;

	$count_choices = count( $field_submit );
	if ( $count_choices < 2 ) {
		wpforms()->process->errors[ $form_data['id'] ][ $field_id ] = 'Please select at least 2 options';

	}

}
add_action( 'wpforms_process_validate_checkbox', 'wpf_checkbox_validation', 10, 3 );

Please remember to update the code above to match your form ID and field ID. If you need any help finding those ID numbers, please review this documentation.

Now if a visitor tries to submit the form with only one selection, they will see this error.

Visitors will see an error message below the field if the user hasn't selected the minimum number of choices

And that’s all you need to set a min and max number of choices on your checkboxes. Would you like to customize the look of these checkboxes? Take a look at our article on How to Customize Checkbox and Radio Fields to Look Like Buttons.

FAQ

Q: Can I use this only a specific form and a specific field ID?

A: Absolutely, to use this code on a specific form, use this code snippet:

function wpf_dev_checkbox_validation( $field_id, $field_submit, $form_data ) {
	
	// Change the number to match your form ID
	if ( absint( $form_data['id'] ) !== 1289 ) {
        return $field_id;
    }
	
		$field_submit  = (array) $field_submit;
		$count_choices = count( $field_submit );
		if ( $count_choices < 2 ) {
			wpforms()->process->errors[ $form_data['id'] ][ $field_id ] = 'Please select at least 2 options';
		}
}
add_action( 'wpforms_process_validate_checkbox', 'wpf_dev_checkbox_validation', 10, 3 );

If you would like to target a specific form and a specific field ID, use this code snippet.

function wpf_dev_checkbox_validation( $field_id, $field_submit, $form_data ) {
	
	// Change the number to match your form ID
	if ( absint( $form_data['id'] ) !== 1289 ) {
        return $field_id;
    }
	
	// Change this number to match the field ID
	if (absint($field_id) === 15 ) {
		$field_submit  = (array) $field_submit;
		$count_choices = count( $field_submit );
		if ( $count_choices < 2 ) {
			wpforms()->process->errors[ $form_data['id'] ][ $field_id ] = 'Please select at least 2 options';
		}
	}
}
add_action( 'wpforms_process_validate_checkbox', 'wpf_dev_checkbox_validation', 10, 3 );

Action reference: wpforms_process_validate_checkbox