1

I know how to make a form validation for each of inputs in codeigniter. But is it possible to make a validation for multiple checkbox where the user should select at least one check box?

example

<input type=checkbox name=days[] >Mon
<input type=checkbox name=days[] >Tues
<input type=checkbox name=days[] >Wed
<input type=checkbox name=days[] >Thur

where the codeigniter should validate that the user should select at least one checkbox.?

3 Answers 3

5

HTML:

<input type=checkbox name=days[] >Mon
<input type=checkbox name=days[] >Tues
<input type=checkbox name=days[] >Wed
<input type=checkbox name=days[] >Thur

Use following code to validate above HTML :

 $this->form_validation->set_rules('days[]', 'days', 'required');
Sign up to request clarification or add additional context in comments.

Comments

1

Use following codes:

$this->form_validation->set_rules('days', 'days', 'required');

Comments

1
// Check if user has selected at least one checkbox manually
if ( isset($_POST['days'])
{
//some validation errors
}

or you can simply do it using validation rules like

$this->form_validation->set_rules('days', 'Days Label', 'required');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.