Tuesday, November 22, 2011

Validation For Checking If User Is Above 18 Years Of Age

This code block returns if user is above 18 years of age. it also have checks for leap year and all. Although it is written in php but logic can be used in other platforms also.


//for calculating birthdate.
$birth_day = $_GET["day"];
$birth_month = $_GET["month"];
$birth_year = $_GET["year"];
$DATE = $birth_year . $birth_month . $birth_day;
function CheckAge($day, $month, $year)
{
$QCPASS=0;
if((empty($day))&&(empty($month))&&(empty($year))){
echo "atleast put the date, man!! :@";
$QCPASS=1;
}

elseif((!empty($day))&&(!empty($month))&&(!empty($year))){
if($day<1||$day>31){
echo "day is not valid. R u nuts ?? ";
$QCPASS=1;
}
elseif(($month<1||$month>12)&&$month!=2){
echo "month is not valid. may god help u.....";
$QCPASS=1;
}
elseif($month==2){
if($year%4==0&&$day>29)
{
echo "only till 29 in feb";
$QCPASS=1;
}
elseif($year%4!=0&&$day>28){
echo "only till 28 not a leap year bro...";
$QCPASS=1;
}
}
elseif($month==4||$month==6||$month==9||$month==11){
if($day>30){
echo "some months have 30 days only. sorry i cant change my calendar for u.";
$QCPASS=1;
}
}
if($QCPASS==0){

{
$NOW_year = gmdate('Y');
$NOW_month = gmdate('m');
$NOW_day = gmdate('d');

if (($NOW_year - $year) > 18){
echo "fine,u r above 18. do watever u lyk dude.....";
}
elseif ((($NOW_year - $year) == 18) && ($NOW_month > $month)){
echo "fine,u r above 18. do watever u lyk dude.....";
}
elseif ((($NOW_year - $year) == 18) && ($NOW_month == $month) && ($NOW_day >= $day)){
echo "fine,u r above 18. do watever u lyk dude.....";
}
else{
echo "mummy se bolo complan pilayein"; //if err exist
}
}
}
}
else if((!empty($day))||(!empty($month))||(!empty($year))){
echo "LOL......u missed some fields above. :D ";
}


}

CheckAge($birth_day, $birth_month, $birth_year);

?>

No comments:

Post a Comment