Andre
Andre

Reputation: 9170

How to validate that a specific ammount of checkboxes are checked in PHP

I have to validate that maximum 3 checkboxes are clicked. There are 11. How could I do this efficiently and without testing every possible situation?

Upvotes: 1

Views: 153

Answers (1)

Sarfraz
Sarfraz

Reputation: 382656

You can do like this:

if (count($_POST['checkbox_name']) === 3)
{
  // your code here.....
}

where your checkbox names should be suffixed with [] eg:

<input type="checkbox" name="checkbox_name[]" value="1" />
<input type="checkbox" name="checkbox_name[]" value="2" />
<input type="checkbox" name="checkbox_name[]" value="3" />

Upvotes: 2

Related Questions