Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
Thread: get age from dob
10-02-2013, 06:19 AM #1
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.
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.
Last edited by durangod; 10-03-2013 at 12:23 AM.
10-02-2013, 06:33 PM #2
- Join Date
- Dec 2010
- 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;
10-03-2013, 12:23 AM #3
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
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 thatCode:onblur="return gettheage();"
Yes you are very correct and thanks for sharing that, i have ammended my code above as well to reflect the
Thanks so much DevCode:var curmo = todaysdate.getMonth() + 1;
10-04-2013, 09:36 AM #4
- Join Date
- Jun 2002
- London, England
- Thanked 2,573 Times in 2,551 Posts
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.