0

I am doing codeigniter validation for email id. I have declared 3 text box for entering email id. I am retrieving all the 3 email id's in array called ref_email. now I want to do email id validation only when email id text boxes are not empty and show error message in view page. if email id text boxes are empty, then i don't want to do any validation. I don't know where i have done mistake in my code.

My controller code:

    $ref_email_array=$this->input->post('ref_email');

            foreach($ref_email_array as $key => $r_email ){
        if ($r_email!="") {
                $this->form_validation->set_rules('ref_email['.$key.']', 'Email','trim|valid_email');
                $this->form_validation->set_error_delimiters('<div class="error_user" style="color:red">', '</div>');
                }
            }
           if($this->form_validation->run()){   // insertion code }

can anybody help me? thanks in advance.

4
  • why not use php empty() function to check whether input array is empty or not? Commented Jan 21, 2015 at 9:13
  • I just want to check only when array value is not empty.. I am using 3 text boxes so my array always return 0,1,2 index, so array wont be empty. Commented Jan 21, 2015 at 9:15
  • Use array_filter to check if all keys have value or not. Check out this link Commented Jan 21, 2015 at 9:21
  • okay i will try and let you know. there is no use of using array filter Commented Jan 21, 2015 at 9:26

1 Answer 1

0
$ref_email_array=$this->input->post('ref_email');
foreach($ref_email_array as $r_email ){
if (empty($r_email)) {}
else  {
$this->form_validation->set_rules('ref_email['.$key.']','Email','trim|valid_email');
$this->form_validation->set_error_delimiters('<div class="error_user" style="color:red">', '</div>');
      }
 }

I hope this might help you ! , it works for me

Sign up to request clarification or add additional context in comments.

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.