Go Back   Webmaster Forums UK SEO SEM Webmaster Community Forum - UKWW > Web Design and Website Development > Programming > PHP Forum
Register FAQ Members List Downloads Calendar Today's Posts Webmaster Resources Webmaster Blogs
 
 

PHP Forum PHP related discussion, help, tips and tutorials.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-25-2008, 07:33 PM
dman_2007
Guest
 
Posts: n/a
Default Tip : Validating age and Date of birth

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.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
Reply

Bookmarks



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Webmaster Resources
UK WW SEO Tools
Find UK Hosts
 
The Forum Rules
Forum Rules - MUST READ
 
Site Of the Month
BizzFace
Nominate site of the month
 
Tag Cloud
ad exchange affiliates audio ads bid bidding directory bid directory bid directory list bid for position business clothes contest cricket writer dedicated servers delisted designer discount ecommerce executive suites finance google handbags internet and marketing iphone jewelry link exchange link submissions nokia n95 paid blogging phones electronics pr7 directory replica samsung seo seo company social bookmark sunglasses uk property writer wallets web web design web development website website development website promotion wholesale yahoo backlink

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 08:37 AM.

UK Webmaster World Forums - Internet marketing, web development, domain names, SEO contest and discussuons.
Subscribe to our feeds   Subscribe to our feeds

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0