### [Setting a Minimum Number of Choices for a Checkbox](https://wpforms.com/developers/how-to-set-a-minimum-number-of-choices-for-a-checkbox/)

**Published:** November 18, 2020
**Author:** Umair Majeed

**Excerpt:** This tutorial will show you how using PHP will enable you to provide a minimum limit of Checkbox fields that would need to be checked before processing the form. 

**Content:**

Would you like to set a minimum number of choices for your **Checkbox** form field? WPForms currently supports limiting the number of choices for the [Checkbox field](https://wpforms.com/docs/how-to-limit-the-number-of-checkboxes-selections/). However, with a custom snippet, you’ll be able to set a minimum number for these choices using a small PHP code snippet.

In this tutorial, we’ll walk you through the steps to set up your **Checkbox** form field to require a minimum number of selections.

---

Before getting started, make sure WPForms is [installed and activated](https://wpforms.com/docs/install-wpforms-plugin/ "Installing the WPForms Plugin") on your WordPress site and that you’ve [verified your license](https://wpforms.com/docs/verify-wpforms-license/ "Verifying Your License").

## Creating the Form

To begin, you’ll need to [create a new form](https://wpforms.com/docs/creating-first-form/) or edit an existing one to access the form builder. In the form builder, make sure to include at least one **Checkbox** field to your form.

![create your form and add your checkbox form field](https://wpforms.com/wp-content/uploads/2020/11/wpforms-min-number-choices-checkbox.jpg)## Adding the Snippet to Set a Minimum Number

To set a minimum number of choices, you’ll need to add this code snippet below to your site. If you need any assistance in adding code snippets to your site, please [review this tutorial.](https://wpforms.com/developers/how-to-add-custom-php-or-javascript-for-wpforms/ "How to Add Custom PHP or JavaScript for WPForms")

```

/**
 * Set minimum number of choices for a checkbox for the field
 *
 * @link https://wpforms.com/developers/how-to-set-a-minimum-number-of-choices-for-a-checkbox/
 */
 
function wpf_checkbox_validation( $field_id, $field_submit, $form_data ) {
          
    $field_submit  = (array) $field_submit;
  
    // Make sure we have an array of choices and count the number of choices.
    $count_choices = is_array( $field_submit ) ? count( $field_submit ) : 0;
  
    if ( $count_choices < 2 ) {
        wpforms()->process->errors[ $form_data[ 'id' ] ][ $field_id ] = esc_html__( 'Please select at least 2 options', 'your-text-domain' );
    }
  
}
  
add_action( 'wpforms_process_validate_checkbox', 'wpf_checkbox_validation', 10, 3 );
```

In the code above, any checkbox will require a minimum of **two** choices. If you’d like to update the minimum number, change the value in the if statement.

**Note:** This snippet will apply to the Checkbox field on all forms on your site.

When a user selects fewer than **two** choices on your form, a message will be displayed under the **Checkbox** field when the form is submitted.

![using this snippet to set a minimum number of choices, the users will see an error and the form will not submit when the minimum options have not been met ](https://wpforms.com/wp-content/uploads/2020/11/wpforms-checkbox-minimum.jpg)## Setting a Limit for Multi-page Forms

When using [multi-page forms](https://wpforms.com/docs/how-to-create-multi-page-forms-in-wpforms/ "Creating Multi-Page Forms"), you may not want to wait for the form submission before displaying the error.

You can easily show a warning message under the **Checkbox** field when the **Next** button is clicked for multi-page forms.

To use this option, simply add this code snippet to your site instead.

```

/**
 * Set minimum number of choices for a checkbox form field
 *
 * @link https://wpforms.com/developers/how-to-set-a-minimum-number-of-choices-for-a-checkbox/
 */
function wpf_dev_min_checkbox() {
    ?>

**Categories:** Tutorials

**Tags:** PHP

---

