Its a callback function for codeigniter Form validation class. It will check, whether the input date is a valid date, and it will also check whether the user is above a particular age limit. Here its 13 years.
You can use this function in codeigniter as given below
load form validation class for codeigniter into your controller functions
$this->load->library('form_validation');
Set the rules for date of birth field
$config = array(
array(
'field' => 'dob',
'label' => 'date of birth',
'rules' => 'trim|required|callback_dob_valid'
),
);
if you need you can add additional rules
Set the Rule for your form
$this->form_validation->set_rules($config);
then run the form against these rules
if ($this->form_validation->run() == FALSE)
{
// reload the view
}
else
{
// success action
}
/**
* Date of birth
* Checking for a valid date for date of birth and age verification
*
* @author http://phpqa.in
* @since
* @copyright GPL
*/
function dob_valid($str)
{
if($str)
{
//match the format of the date dd-mm-yyyy
// for yyyy-mm-dd use "/^([0-9]{4})-([0-9]{2})-([0-9]{4})$/
if (preg_match ("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $str, $parts))
{
//check weather the date is valid of not
$day = $parts[1];
$month = $parts[2];
$year = $parts[3];
/*
// for yyyy-mm-dd
$day = $parts[3];
$month = $parts[2];
$year = $parts[1];
*/
// checking 4 valid date
if(!checkdate($month,$day,$year))
{
$this->form_validation->set_message('dob_valid', '%s is not a valid date, use dd-mm-yyyy format');
return false;
}
else
{
//check for future date
$dob = $year.'-'.$month.'-'.$day;
if($dob < date("Y-m-d"))
{
// age verification using DOB
$dob_time = mktime(0, 0, 0, $month,$day,$year);
$age_req = strtotime('+13 years', $dob_time);
if(time() < $age_req)
{
$this->form_validation->set_message('dob_valid', 'You must be atleast 13 years old');
return false;
}
else
{
return true;
}
return true;
}
else
{
$this->form_validation->set_message('dob_valid', '%s should be a past date');
return false;
}
}
}
else
{
$this->form_validation->set_message('dob_valid', '%s is not a valid date, use dd-mm-yyyy format');
return false;
}
}
return false;
}
By removing the "$this->form_validation->set_message()" function from the above function we can use the above function in any php scripts
You can use this function in codeigniter as given below
load form validation class for codeigniter into your controller functions
$this->load->library('form_validation');
Set the rules for date of birth field
$config = array(
array(
'field' => 'dob',
'label' => 'date of birth',
'rules' => 'trim|required|callback_dob_valid'
),
);
if you need you can add additional rules
Set the Rule for your form
$this->form_validation->set_rules($config);
then run the form against these rules
if ($this->form_validation->run() == FALSE)
{
// reload the view
}
else
{
// success action
}
/**
* Date of birth
* Checking for a valid date for date of birth and age verification
*
* @author http://phpqa.in
* @since
* @copyright GPL
*/
function dob_valid($str)
{
if($str)
{
//match the format of the date dd-mm-yyyy
// for yyyy-mm-dd use "/^([0-9]{4})-([0-9]{2})-([0-9]{4})$/
if (preg_match ("/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/", $str, $parts))
{
//check weather the date is valid of not
$day = $parts[1];
$month = $parts[2];
$year = $parts[3];
/*
// for yyyy-mm-dd
$day = $parts[3];
$month = $parts[2];
$year = $parts[1];
*/
// checking 4 valid date
if(!checkdate($month,$day,$year))
{
$this->form_validation->set_message('dob_valid', '%s is not a valid date, use dd-mm-yyyy format');
return false;
}
else
{
//check for future date
$dob = $year.'-'.$month.'-'.$day;
if($dob < date("Y-m-d"))
{
// age verification using DOB
$dob_time = mktime(0, 0, 0, $month,$day,$year);
$age_req = strtotime('+13 years', $dob_time);
if(time() < $age_req)
{
$this->form_validation->set_message('dob_valid', 'You must be atleast 13 years old');
return false;
}
else
{
return true;
}
return true;
}
else
{
$this->form_validation->set_message('dob_valid', '%s should be a past date');
return false;
}
}
}
else
{
$this->form_validation->set_message('dob_valid', '%s is not a valid date, use dd-mm-yyyy format');
return false;
}
}
return false;
}
By removing the "$this->form_validation->set_message()" function from the above function we can use the above function in any php scripts