Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,487
    Thanks
    279
    Thanked 32 Times in 31 Posts

    get age from dob

    This client wanted a max age of 25 on their site so i tossed this together for them, it seems to work. Im sure their maybe a quicker way but this looks and seems to be pretty solid... Please correct if you see anything..

    I did test this numerious times and seems to work well.

    Thanks..

    note, my dob is not entered in one input, it is entered in three sep inputs so i already luckily have it split up for me.


    Code:
    <script type="text/javascript">
    function gettheage() 
    {
        var subone = 0;
    
        /* get cur date */
        var todaysdate = new Date();
    
         /* get values dd mm yyyy of bd */
         var dobday = document.getElementById('lstDay').value;
         var dobmo = document.getElementById('lstMonth').value;
         var dobyear = document.getElementById('txtYear').value;  /* last two only */
         var dobyear = '19' + dobyear; /* concat the 19 to the front */
    
        /* get values of dd mm yyyy of todays date */
        var curday = todaysdate.getDate();
        var curmo = todaysdate.getMonth() + 1;
        var curyr = todaysdate.getFullYear();
    
       /* the calc of age  below assumes this year also */
       /* now lets find out if we need to subtract one for this year or not */
    
         /* if curent month is greater than dob month */
         /* do nothing you have already assumed this year so leave it be */
    	 
    	 
    	 /* if curent month is less than the dob month then subtract one for this year */ 
    
        if(curmo < dobmo)
        {	  
         var subone = -1;	  
        }else{
                  /* if current month is equal to dob month then check the day */        
                 if(curmo == dobmo)
                 { 
                 
                     /* if the curent day is geater or eq to the dob day then leave it be */
                     /* you have already assumed this year */
                     /* if current day is less than dob day then subtract this year */
                     if(curday < dobday)
                     {
                     var subone = -1;
                     }//close if
                  }//close if
    
               }//close else
               
    /* subtract dob year from current year plus subone if necessary to get the age  */
    
      var theirage = (curyr - dobyear) + subone;  /* adding -1 or 0  */
      
      if(theirage > 25)
      {
       /* post the over age in the auto calculate input and go back to the birthday day */ 
       document.getElementById('newmemage').value = theirage;    
       alert("Sorry but Maximum age for this website is 25 years old");
       document.FrmRegister.lstDay.focus();
       return (false);
      }else{
            /* post the age in the auto calculate input and move to the next field*/ 
            document.getElementById('newmemage').value = theirage; 
            document.getElementById('lstSex').focus(); 
           }//close else
    
    return (true);  
    }//close function gettheage
    happy coding... peace
    Last edited by durangod; 10-03-2013 at 12:23 AM.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    I think your code has at least one flaw, but a quite major one. Did you really test it for "borderline" values (like entering a dob that is exactly 25 years ago)?

    So I tested your code with October 2, 1988. A person born on that date would be 25 exactly. But your code shows 24. And so it does for September 3, 1988. It shows 25 for September 2, 1988 .... which is 25 years and 1 month ago.

    And that is exactly the issue. todaysdate.getMonth() will return months between 0 and 11 (and not 1 to 12 as expected). So it will always be one month off

    Try this line
    Code:
    var curmo = todaysdate.getMonth() + 1;

  • #3
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,487
    Thanks
    279
    Thanked 32 Times in 31 Posts
    I did test it on days before, month before, and days after and month after as well. But you are quite corrrect, and i know how i missed this.

    I had my

    Code:
    onblur="return gettheage();"
    on the year input so i could change the month or day and it would not change so when it showed me 25 or 24 i just happend to of changed the month or day and it was correct. That one got thru the cracks on me... sorry about that

    Yes you are very correct and thanks for sharing that, i have ammended my code above as well to reflect the

    Code:
    var curmo = todaysdate.getMonth() + 1;
    Thanks so much Dev

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,316
    Thanks
    203
    Thanked 2,565 Times in 2,543 Posts
    Code:
    <script type = "text/javascript">
    
    var today = new Date();
    var nowyr = today.getFullYear();
    var dob = prompt("Enter your age in DD/MM/YYYY format","");
    
    var dobsplit = dob.split("/");
    var byr = dobsplit[2];
    var bmth = dobsplit[1];
    var bday = dobsplit[0];
    var age = nowyr-byr;
    var d = new Date(nowyr,bmth-1,bday);  // months in Javascript are 0-11
    if (d > today) {  // birthday not yet reached
    age --;
    }
    
    if ((isNaN(age)) || (age < 0)){
    alert ("Invalid data!");
    }
    
    else {alert ("Your age is " + age);
    }
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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