The Most Active and Friendliest
Affiliate Marketing Community Online!

“Propeller”/  MyBid

Tip : Validating age and Date of birth

D

dman_2007

Guest
Every now and then you come across the need to gather date of birth information from users and to validate their age against the minimum required age to access the website, here's the code which will help you do that, we'll collect the dob information in the following format YYYY-MM-DD using an input form control :

Code:
 function valid_age($dob, $age)
 {
   $today = getdate();
   
   if($today['year'] < $dob['year'])
   {
     return false;
   }
   else
   {
     if($today['mon'] > $dob['mon'])
     {
       if(($today['year'] - $dob['year']) >= $age)
       {
         return true;
       }
       else
       {
         return false;
       }
     }
     else if($today['mon'] > $dob['mon'])
     {
       if(($today['year'] - $dob['year'] - 1) >= $age)
       {
         return true;
       }
       else
       {
         return false;
       }
     }
     else
     {
       if($today['mday'] > $dob['mday'])
       {
         if(($today['year'] - $dob['year']) >= $age)
         {
           return true;
         }
         else
         {
           return false;
         }
       } 
       else if($today['mday'] < $dob['mday'])
       {
         if(($today['year'] - $dob['year'] - 1) >= $age)
         {
           return true;
         }
         else
         {
           return false;
         }
       }
       else
       {
         if(($today['year'] - $dob['year']) >= $age)
         {
           return true;
         }
         else
         {
           return false;
         }
       }
     }
   }  
 }
 
 $_POST['dob'] = trim($_POST['dob']);
 if(!preg_match('/(\d{4})\-(\d{1,2})\-(\d{1,2})/', $_POST['dob'], $dob_comps))
 {
   $show_error  = 1;
 }
 else if(!checkdate($dob_comps[2], $dob_comps[3], $dob_comps[1]))
 {
   $show_error = 1;
 }
 else if  (!valid_age(array('mon' => $dob_comps[2], 'mday' => $dob_comps[3], 'year' => $dob_comps[1]), 13))
 {
   $show_error = 1;
 }
 else
 {
   $clean_data['dob'] = $_POST['dob']; 
 }

Try figuring out how this code works on you, if you can't understand on your own then don't worry i'll explain it in my next post.
 
Thanks!

Hi,

Thank you so much for sharing this, it’s really useful.

Cheers,


Maneet Puri
Lexolution IT Services
 
Is it possible to change the format that the date it entered in, or does it have to conform to the format you mention?
 
MI
Back