Facebook

Monday, August 15, 2011

Code Igniter Date Validation Function

These two functions i use to validate maximum and minimum values of a date field


FUNCTION MAXDATE:
Function Name  : maxDate
Parameters     :$str,$reference,$refName

Meaning :
 $str : This variable is used to pass the variable name used to post the value to be validated
 $reference : This variable is used to pass the value of date that should be the allowable maximum date.
$refName : This variable is used to pass the name of reference, is Optional
Usage: 
Can be used in two ways
      1.   As call back function to the validation rules of Codeigniter
      Example: 
         $this->form_validation->set_rules('sessionDate['.$i.']','Date', 'trim|callback_maxDate['.$endDate.']|xss_clean');
      2.   As function Call
                  if(!maxDate('startDate','2011-06-23','End Date')){
             echo 'Satrt Date Should be Before End Date';
         }
---------------------------------------------------------------------
function maxDate($str,$reference,$refName='Reference Date'){
if ( !preg_match('/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/', $reference) ) {
//WRONG REFERENCE DATE FORMAT
$this->form_validation->set_message('maxDate', $refName.' is in Unknown Format');
//echo $refName.' is in Unknown Format';
return false;
}
else if ( !preg_match('/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/', $str) ) {
//WRONG DATE FORMAT
$this->form_validation->set_message('maxDate', 'The %s shoyuld be in "YYY-MM-DD" Format.');
//echo 'The %s shoyuld be in "YYY-MM-DD" Format.';
return false;
}
        
$arr = explode("-", $str);    // splitting the array
$yyyy = $arr[0];            // first element of the array is year
$mm = $arr[1];              // second element is month
$dd = $arr[2];              // third element is days
if( !checkdate($mm, $dd, $yyyy) ){
$this->form_validation->set_message('maxDate', 'The %s is an Invalid Date.');
//echo 'The %s is an Invalid Date.';
return false;
}
else if(strtotime($str)>strtotime($reference)){
$this->form_validation->set_message('maxDate', 'The Maximum value of %s should be '.$reference.'.');
//echo 'The Maximum value of %s should be '.$reference.'.';
return false;
}

//echo $this->form_validation->set_message('maxDate', 'The %s is an Invalid Date.');
return true;
    }
--------------------------------------------------------

FUNCTION MINDATE:


Function Name  : minDate
Parameters     :$str,$reference,$refName

Meaning :
 $str : This variable is used to pass the variable name used to post the value to be validated
 $reference : This variable is used to pass the value of date that should be the allowable minimum date.
$refName : This variable is used to pass the name of reference, is Optional
Usage: 
Can be used in two ways
      1.   As call back function to the validation rules of Codeigniter
               Example: 
         $this->form_validation->set_rules('sessionDate['.$i.']','Date', 'trim|callback_minDate['.$endDate.']|xss_clean');
      2.   As function Call
                  if(!minDate('endDate','2011-06-23','Start Date')){
             echo 'End Date Should be Before Start Date';
         }


function minDate($str,$reference,$refName='Reference Date'){
if ( !preg_match('/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/', $reference) ) {
//WRONG REFERENCE DATE FORMAT
$this->form_validation->set_message('minDate', $refName.' is in Unknown Format');
return false;
}
else if ( !preg_match('/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/', $str) ) {
//WRONG DATE FORMAT
$this->form_validation->set_message('minDate', 'The %s shoyuld be in "YYY-MM-DD" Format.');
return false;
}
        
$arr = explode("-", $str);    // splitting the array
$yyyy = $arr[0];            // first element of the array is year
$mm = $arr[1];              // second element is month
$dd = $arr[2];              // third element is days
if( !checkdate($mm, $dd, $yyyy) ){
$this->form_validation->set_message('minDate', 'The %s is an Invalid Date.');
return false;
}
else if(strtotime($str)<strtotime($reference)){
$this->form_validation->set_message('minDate', 'The Minimum value of %s should be '.$reference.'.');
return false;
}

//$this->form_validation->set_message('minDate', 'The %s is an Invalid Date.');
return true;
    }

No comments:

Post a Comment