Bajrang
Bajrang

Reputation: 8629

form input field for date in codeigniter

Is there codeigniter provides any js class for taking date value in form field from the user ? I am searching in codeigniter's user guide but cant finding such class.

Currently i am using a another js class for taking date value.

Upvotes: 2

Views: 3271

Answers (1)

Laurence
Laurence

Reputation: 60058

You can extend Form_validation and run a callback

// Is this a valid date?
function valid_date($str) 
{
    $CI =& get_instance();
    $CI->form_validation->set_message('valid_date', 'Sorry - the %s field must be a date with format dd-mm-yyyy');

    if (preg_match("/^(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-(19|20)[0-9]{2}$/", $str))
    {
        $arr = explode("-", $str);
        $yyyy = $arr[2];
        $mm = $arr[1];
        $dd = $arr[0];
        if (is_numeric($yyyy) && is_numeric($mm) && is_numeric($dd))
        {
            return checkdate($mm, $dd, $yyyy);
        }
        else
        {
            return false;
        }
    } 
    else
    {
        return false;
    }
}

so this way you can run validation on the field, regardless if the user has JS turned on or not

Upvotes: 1

Related Questions