Monday, May 19, 2008

validate date by php script

Hi

Below is the code for validate date in php (server side validations).



/************************************************

function to check if date entered by date control

is valid date or not

************************************************/

function validdate($tmpdate,$id,$action,$backpg)

{

list($year, $month, $day)=split('[/,.-]', $tmpdate);

$date=$year."-".$month."-".$day;



if(strlen($day)>2)

{

$msg= "Invalid date formats: $tmpdate";

header("location:$backpg?msg=$msg&id=$id&action=$action");

exit;

}



if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $tmpdate, $regs))

{

$a= strlen("$regs[3]");

if($a>2)

{

$msg= "Invalid date formats: $tmpdate";

header("location:$backpg?msg=$msg&id=$id&action=$action");

exit;

}

elseif($regs[2]>12)

{

$msg= "Invalid date format: $tmpdate";

header("location:$backpg?msg=$msg&id=$id&action=$action");

exit;

}

elseif($regs[3]>31)

{

$msg= "Invalid date format: $tmpdate";

header("location:$backpg?msg=$msg&id=$id&action=$action");

exit;

}

}

else

{

$msg= "Invalid date format: $tmpdate";

header("location:$backpg?msg=$msg&id=$id&action=$action");

exit;



}//closing else



}