Title: Checkbox validation
Last modified: August 21, 2016

---

# Checkbox validation

 *  [Red Deer Web Design](https://wordpress.org/support/users/bdbolin/)
 * (@bdbolin)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/checkbox-validation/)
 * Hey guys,
 * I have a list of checkboxes and am wanting to build in some kind of loop so I
   don’t have to list every single checkbox. Currently I just have 2 to test with:
 *     ```
       // Sanitize and validate input. Accepts an array, return a sanitized array.
       	function lc_options_validate($input) {
       		// Our first value is either 0 or 1
       		$input['option1'] = ( isset($input['option1']) == 1 ? 1 : 0 );
       		$input['option2'] = ( isset($input['option2']) == 1 ? 1 : 0 );
   
       		return $input;
       	}
       ```
   
 * I am drawing a blank, any ideas on how I can have this loop?
    Thanks!

Viewing 1 replies (of 1 total)

 *  [tormorten](https://wordpress.org/support/users/tormorten/)
 * (@tormorten)
 * [11 years, 9 months ago](https://wordpress.org/support/topic/checkbox-validation/#post-5138255)
 * Hi
 * If you were to build the checkboxes like this, PHP would read them as an array(`
   $_POST['my-checkbox']`) where only the checked elements would exist and have 
   a value of “1”:
 *     ```
       <label for="my-checkbox-1"><input type="checkbox" name="my-checkbox[]" value="1" id="my-checkbox-1" /> Checkbox 1</label>
       <label for="my-checkbox-2"><input type="checkbox" name="my-checkbox[]" value="1" id="my-checkbox-2" /> Checkbox 2</label>
       <label for="my-checkbox-3"><input type="checkbox" name="my-checkbox[]" value="1" id="my-checkbox-3" /> Checkbox 3</label>
       <label for="my-checkbox-4"><input type="checkbox" name="my-checkbox[]" value="1" id="my-checkbox-4" /> Checkbox 4</label>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Checkbox validation’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [tormorten](https://wordpress.org/support/users/tormorten/)
 * Last activity: [11 years, 9 months ago](https://wordpress.org/support/topic/checkbox-validation/#post-5138255)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
