www.webdeveloper.com
Results 1 to 3 of 3

Thread: How can I check someones age?

  1. #1
    Join Date
    Dec 2004
    Posts
    439

    How can I check someones age?

    Hi, using js validation, if a user enters a birth date with drop down boxes, how can i check to see if he is over 13 years old, thanks in advance.

    these are my drop down boxed (php page)

    PHP Code:
    Enter Birth Date<br />
            <select name="user_bd_mon">
            <option value=''></option>
            <option value='1'>January</option>
            <option value='2'>February</option>
            <option value='3'>March</option>
            <option value='4l'>April</option>
            <option value='5'>May</option>
            <option value='6'>June</option>
            <option value='7'>July</option>
            <option value='8'>August</option>
            <option value='9'>September</option>
            <option value='10'>October</option>
            <option value='11'>November</option>
            <option value='12'>December</option>
            </select>&nbsp;
            <select name="user_bd_day">
            <option value=''></option>
            <?php for($d 1$d <= 31$d++){ ?>
            <option value="<?php echo $d?>"><?php echo $d?></option>
            <?php ?>
            </select>&nbsp;
                <select name="user_bd_year">
                <option value=''></option>
            <?php for($y date("Y"); $y >= 1970$y--){ ?>
            <option value="<?php echo $y?>"><?php echo $y?></option>
            <?php ?>
            </select>

  2. #2
    Join Date
    Jan 2005
    Location
    San Diego, CA
    Posts
    4,887

  3. #3
    Join Date
    Jan 2006
    Location
    I'm in GMT -5
    Posts
    561
    decibel:

    Test this as a .php document. I did and it works in IE and Firefox.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    		
    	var isToday = "";
    
    	function setDateLimit(isForm,isList,nDays){
    		
    		var prevLimit = isForm[isList].length;
    		var currLimit = nDays;
    		if (currLimit < prevLimit)
    			{isForm[isList].length = currLimit}
    		else {
    		      nOptions = currLimit-prevLimit
    		      for (i=1; i<nOptions+1; i++)
    			{
    			 var isText = (prevLimit+i).toString();
    			 insertDates(isForm[isList],isText,isText);
    			}
    		     }
    		isForm[isList].selectedIndex = 0;
    		checkLeap(isForm,isList);
    	}
    	
    	function insertDates(isList,isValue,isText){
    
    		var nOption = document.createElement('option'); 
    		var isData = document.createTextNode(isText); 
    		nOption.setAttribute('value',isValue); 
    		nOption.appendChild(isData); 
    		isList.appendChild(nOption); 
    	}
    
    	function checkLeap(isForm,isList){
    
    		var isYear = isForm.startYears.value;
    		if (isYear % 4 == 0 && isForm['startMonths'].value == '28')
    			{
    			 nOption = document.createElement('option'); 
    			 isData = document.createTextNode('29'); 
    			 nOption.setAttribute('value','29'); 
    			 nOption.appendChild(isData); 
    			 isForm[isList].appendChild(nOption);
    			}
    	}
    
    	function resetBoth(isForm,isMonths,isDates){
    
    		isForm[isMonths].selectedIndex = 0;
    		isForm[isDates].options.length = 0;
    		for (i=1; i<32; i++)
    			{
    			 insertDates(isForm[isDates],i,i)
    			}
    	}
    
    	function clipDates(isForm,isMonth,isList){
    
    		var nDates = isForm[isMonth].value;
    		if (nDates == '30')
    			{isForm[isList].options.length = 30}
    		if (nDates == '28')
    			{isForm[isList].options.length = 28}
    		if (isForm.startYears.value % 4 == 0 && isForm[isMonth].value == '28')
    			{
    			 var nOption = document.createElement('option'); 
    			 var isData = document.createTextNode('29'); 
    			 nOption.setAttribute('value','29'); 
    			 nOption.appendChild(isData); 
    			 isForm[isList].appendChild(nOption); 
    			}
    	}
    
    	function setUp(){
    
    		isToday = new Date();
    		var nStart = isToday.getMonth()+1+"/"+isToday.getDate()+"/"+isToday.getFullYear();
    		var isStart = nStart.split("/");
    		document.forms[0].startMonths.selectedIndex = isStart[0]-1;
    		document.forms[0].startDates.selectedIndex = isStart[1]-1;
    		var yearOffset = (1970-isStart[2])*-1;
    		document.forms[0].startYears.selectedIndex = yearOffset;
    		clipDates(document.forms[0],'startMonths','startDates');
    		document.forms[0].birthDate.value = nStart;
    	}
    
    	function insertSlashDate(isForm){
    
    		var startStr = isForm.startMonths.selectedIndex+1+"/";
    		startStr += isForm.startDates.value+"/";
    		startStr += isForm.startYears.value;
    		startStr = startStr.replace(/^(\d{1}\/)/,"0$1").replace(/(\d{2}\/)(\d{1}\/)/,"$10$2")
    		isForm.birthDate.value = startStr;
    	}
    
    	function validate(nForm){
    
    		var birthDay = new Date(nForm.birthDate.value);
    		var age = isToday.getFullYear()-birthDay.getFullYear();
    		var currYrbDay = new Date(isToday.getFullYear(),birthDay.getMonth(),birthDay.getDate())
    		if (currYrbDay > isToday && age > 0){age--}
    		if (age < 13){alert('Access denied');return false}
    		alert(nForm.birthDate.value);
    	}
    
    	onload=setUp;
    
    </script>
    </head>
    <body>
    <br>
    <form onsubmit="return validate(this)">
    <Table align='center' cellspacing='0' cellpadding='5' style='font-size:12pt;border:solid black 1px;background-color:lightyellow'>
    <Thead><TH colspan='4' bgcolor='moccasin'> Submit your birthday </TH></Thead>
    
    <td>
    <?php
    
    $months = array (0 => 'January','February','March','April','May','June','July','August','September','October','November','December');
    $dayLimit = array('31','28','31','30','31','30','31','31','30','31','30','31'); 
    
    ?>
    <select name='startMonths' onchange="setDateLimit(this.form,'startDates',this.value);insertSlashDate(this.form)">
    <?php
    foreach ($months as $key => $value) {
      echo "<option value='$dayLimit[$key]'>$value</option>\n";
    }
    ?>
    </select>
    </td>
    
    <td>
    <? $days = range (1, 31) ?>
    <select name='startDates' onchange="insertSlashDate(this.form)"> 
    <?php 
    foreach ($days as $value) {
      echo "<option value=\"$value\"> $value</option>\n";
    }
    ?>
    </select>
    </td>
    
    <td>
    <?php $years = range (1970, 2010) ?>
    <select name='startYears' onchange="resetBoth(this.form,'startMonths','startDates');insertSlashDate(this.form)"> 
    <?php 
    foreach ($years as $value) {
      echo "<option value=\"$value\"> $value</option>\n";
    }
    ?>
    </select>
    </td>
    </tr>
    <tr>
    <td colspan='3' align='middle'>
    <input type='submit' value="Submit">
    </td>
    </tr>
    
    </table>
    <input type='hidden' name='birthDate'>
    </form>
    
    </body>
    </html>
    Last edited by James Gatka; 02-02-2006 at 07:29 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.23807 seconds
  • Memory Usage 2,885KB
  • Queries Executed 13 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (3)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (3)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (70):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates