SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Validating Date of Birth

  1. #1
    Learning... tahirjadoon's Avatar
    Join Date
    Jan 2003
    Posts
    770
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validating Date of Birth

    I need to validate the DOB selected by the user, user should be 13 years of age (Current date - 13) to complete the information on the page. If the user is not 13 then user will be redirected to an error page. I have written a function (below) but i know there must be a much better way of doing this. Function receives 3 parameters; day, month, and year selected by the user for DOB.

    checkDate function called inside the following function checks for the valid date or not.

    function CheckDOBforThirteen(nDay,nMonth,nYear){
    //if error return true other wise return false
    var blnReturn = false;

    //get current day, month and year
    var strDate = new Date;

    var nMonthC = parseInt(strDate.getMonth() + 1);
    var nDayC = parseInt(strDate.getDate());
    var nYearC = parseInt(strDate.getFullYear());

    //change the selected info into date format
    var dSelected = new Date(nMonth+"/"+nDay+"/"+nYear);

    //Calculate date with current year -13, this might become February 29 and also
    //this might not be a leap year, check that we have the valid date 13 years before
    //If it is valid keep it, otherwise add 1 to month and make day as 1. It will become
    //March 1 then
    if(!checkDate(nMonthC+"/"+nDayC+"/"+(nYearC - 13)))
    var dThirteen = new Date(nMonthC+1+"/1/"+(nYearC - 13));
    else
    var dThirteen = new Date(nMonthC+"/"+nDayC+"/"+(nYearC - 13));


    if(dSelected > dThirteen)
    blnReturn = true;

    return blnReturn;
    }

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably helps to filter your input with drop-downs.
    Note: the document.write()s are for abbreviating this demo (probably want to hardcode the options in your own form).
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html> 
    <head> 
    <title>untitled</title> 
    <script type="text/javascript"> 
    
    var numDays = {
                    '1': 31, '2': 28, '3': 31, '4': 30, '5': 31, '6': 30, 
                    '7': 31, '8': 31, '9': 30, '10': 31, '11': 30, '12': 31
                  }; 
    
    function setDays(oMonthSel, oDaysSel, oYearSel)
    { 
    	var nDays, oDaysSelLgth, opt, i = 1; 
    	nDays = numDays[oMonthSel[oMonthSel.selectedIndex].value]; 
    	if (nDays == 28 && oYearSel[oYearSel.selectedIndex].value % 4 == 0) 
    		++nDays; 
    	oDaysSelLgth = oDaysSel.length; 
    	if (nDays != oDaysSelLgth)
    	{ 
    		if (nDays < oDaysSelLgth) 
    			oDaysSel.length = nDays; 
    		else for (i; i < nDays - oDaysSelLgth + 1; i++)
    		{ 
    			opt = new Option(oDaysSelLgth + i, oDaysSelLgth + i); 
    			oDaysSel.options[oDaysSel.length] = opt;
    		} 
    	} 
    } 
    
    function chkDOB(oMonthSel, oDaysSel, oYearSel)
    {
    	var DOB = [
    	oMonthSel.options[oMonthSel.selectedIndex].text ,
    	oDaysSel.options[oDaysSel.selectedIndex].text,
    	oYearSel.options[oYearSel.selectedIndex].text
    		  ];
    	DOB = new Date(DOB.join(', ')).getTime(); //milliseconds in DOB
    	var now = new Date().getTime(); //milliseconds in today's date
    	var milliday = (24 * 60 * 60 * 1000); //milliseconds in one day
    	if ((now - DOB) < ((13 * 365 * milliday) + (4 * milliday))) //compensate for leap year
    	{
    		self.location = 'http://www.drtill.de/wkwh/die1/images/funpictures/underage.gif'; //redirect
    		return false;
    	}
    	else return true;
    }
    
    </script> 
    </head> 
    <body> 
    <form action="javascript&#58;alert('old enough')" method="post" onsubmit="return chkDOB(month,day,year)">
    <strong>My date of birth was </strong>
    <select name="month" id="month" onchange="setDays(this,day,year)"> 
    <option value="1" selected="selected">January</option> 
    <option value="2">February</option> 
    <option value="3">March</option> 
    <option value="4">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> 
    <select name="day" id="day">
    <option value="1" selected="selected">1</option>
    <script type="text/javascript">
    var d = 1;
    while (++d < 32)
    	document.write('<option value="' + d + '">' + d + '</option>');
    </script>
    </select> 
    <select name="year" id="year" onchange="setDays(month,day,this)">
    <script type="text/javascript">
    var y = 1939;
    while (++y < 2003)
    	document.write('<option value="' + y + '">' + y + '</option>');
    </script>
    <option value="2003" selected="selected">2003</option>
    </select>
    <input type="submit" value="next" /> 
    </form> 
    </body> 
    </html>
    Last edited by adios; May 13, 2004 at 00:48.
    ::: certified wild guess :::

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tahirjadoon

    this can be done in a more elegant way with OOP. Let's add a new method to the Date object, which returns the age:

    Code:
    <script>
    
    // getAge
    // caclucates the age in years from birth date
    // usage: birthDate.getAge()
    
    Date.prototype.getAge=function(){
    	var today=new Date();
    	var years=today.getFullYear()-this.getFullYear()-1;
    	if(this.getMonth()>today.getMonth())
    		return years;
    	if(this.getMonth()<today.getMonth() || this.getDate()<=today.getDate())
    		return years+1;
    	return years;
    }
    </script>
    Now you can utilize this method to check if user is older than 13:

    Code:
    <script>
    function checkAge(strBirthDate){
    	var birthdate=new Date(strBirthDate);
    	var age=birthdate.getAge();
    	var message="Birthdate: "+birthdate;
    	message+="\n you are now "+age+" years old";
    	if(age<13)
    		message+="\n too young, sorry";
    	else
    		message+="\n it's ok";
    	alert(message);
    }
    </script>
    
    <h1>Lets test</h1>
    
    Birth date in mm/dd/yyyy format:
    <input name=birthdate onblur="checkAge(this.value)" value="5/13/1991">

  4. #4
    Learning... tahirjadoon's Avatar
    Join Date
    Jan 2003
    Posts
    770
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    below is the modified function.


    function CheckDOBforThirteen( nDay, nMonth, nYear ){
    var cDate = new Date( nYear + 13, nMonth -1, nDay );
    return new Date().getTime() <= cDate.getTime();
    }

Bookmarks

Posting Permissions

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