0

validating a form with that has an input type="date" which takes input mm/dd/yyyy however if i var_dump the results it comes out as yyyy-mm-dd.

1 do I validate the input type "mm/dd/yyyy", or do i validate "yyyy-mm-dd".

---im assuming I validate what the var_dump gives me which is yyyy-mm-dd.

2 More importantly I am having a hard time finding out the simplest way of going about validating date.

---I am not sure how to go about validating the date without going through by hand and checking every single date. Is there some built in functions that I could use, and how would I go about implementing that in with my CI form validations I already have.

function check_registration($post_data)
    {
        // Validations:
        // first_name
        $this->form_validation->set_rules('first_name', 'First Name', "trim|required");
        // last_name
        $this->form_validation->set_rules('last_name', 'Last Name', "trim|required");
        // email
        $this->form_validation->set_rules('email', 'Email', "trim|required|valid_email|is_unique[users.email]");
        // password
        $this->form_validation->set_rules('password', 'Password', "trim|required|min_length[6]");
        // confirm_password
        $this->form_validation->set_rules('confirm_password', 'Confirm Password', "trim|required|matches[password]");
        // DOB
        $this->form_validation->set_rules('DOB', 'Date of Birth', "trim|required");

        // run validations:
        if ($this->form_validation->run() === False)
        {
            // set flash data errors
            $this->session->set_flashdata("reg_first_name_error", form_error('first_name'));
            $this->session->set_flashdata("reg_last_name_error", form_error('last_name'));
            $this->session->set_flashdata("reg_email_error", form_error('email'));
            $this->session->set_flashdata("reg_password_error", form_error('password'));
            $this->session->set_flashdata("reg_confirm_password_error", form_error('confirm_password'));
            $this->session->set_flashdata("reg_DOB_error", form_error('DOB'));
            redirect('/');
        }
        // No errors:
        else
        {
            $this->insert($post_data);
            redirect('/success');

        }
    }

Thanks in advance.
-Ants

1 Answer 1

2
  1. Set your own callback rule

    $this->form_validation->set_rules('DOB', 'Date of Birth', "trim|required|callback_dob_check");
    
  2. Write the function inside same controller. Date validation idea taken from: Using filter_var() to verify date?.

    public function dob_check($str){
        if (!DateTime::createFromFormat('Y-m-d', $str)) { //yes it's YYYY-MM-DD
            $this->form_validation->set_message('dob_check', 'The {field} has not a valid date format');
            return FALSE;
        } else {
            return TRUE;
        }
    }
    

More info on DateTime:createFromFormat http://php.net/manual/en/datetime.createfromformat.php. I would stick to YYYY-MM-DD format, as it's quite often.

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.