In comp.lang.javascript message <e41c7d94-5ad6-4f7d-bf24-3ec40abe6549@m4
4g2000hsc.googlegroups.com>, Fri, 4 Apr 2008 13:19:12, laredotornado
Given select menus named birthMonth, birthDay, and birthYear, how do I
check if the user has selected values such that he/she is at least 18
years old?
Others have failed to read that carefully. You are not asking about the
true age of the user, but about the age implied by the selection. if
that is a reasonable thing to use, it is also reasonable to assume that
the computer date is correctly set.
There may be an inevitable complication if either the current date or
the stated DoB is February 29th and the age is close to 18. You will
need to consider the legislation of the jurisdiction.
This will get an Object corresponding to the start of the current local
day :
D1 = new Date()
D1.setHours(0,0,0,0)
and D0 = new Date(birthYear+18, birthMonth-1, birthDay) gets the start
of the local 18th birthday. Compare them with (D0 >= D1). On second
thoughts, setHours is probably not needed.
That will deal correctly with Summer Time, unlike other possible
methods.
Probably you should validate the selected Y M D as being a good date, or
else control the birthDay menu to have the correct length for the
selected birthYear and birthMonth, in which case they should be placed
in the logical order rather than that favoured in the land of Mr
Frederick Flintstone.
If you are submitting the data, you can submit the local date as a
check.
It's a good idea to read the newsgroup c.l.j and its FAQ. See below.