# get age from dob

• 10-02-2013, 06:19 AM
durangod
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 :)
• 10-02-2013, 06:33 PM
devnull69
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;`
• 10-03-2013, 12:23 AM
durangod
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.

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 :)
• 10-04-2013, 09:36 AM
Philip M
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>```