CodeIgniter: Custom date validation function

posted in: CodeIgniter, Form Validation | 0

Custom callback date validation function in code ignitor.

 

Usage:

$this->form_validation->set_rules('date_to', 'Date', 'required|callback_date_y_m_d');

Fuction:

/**
 * This function is a custom date validation function
 *
 * @param string $value The date value that needs to be formatted
 *
 * @return bool
 */
public
function date_y_m_d($value) {
    if (trim($value) !== '') {
        if (!preg_match('/^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$/', trim($value))) {
            $this - > form_validation - > set_message('date_y_m_d', 'Invalid date');

            return FALSE;
        } else {
            return TRUE;
        }
    } else {
        return TRUE;
    }
}

Leave a Reply