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
    Los Angeles, 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 06: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



Recent Articles