I want to filter all the user who can register in my website. how can i filter the age of the registrant using jquery allowing 18 years old and above, but when the age is 13 to 17 years old they can register but they must check checkbox for parental consent. I am using a textbox with mm/dd/yyyy format.
+2
A:
Create 3 date objects - their birthday, a 13 year olds birthday and an 18 year olds birthday for them to be 18/13 today.
var input = document.get..
var dobArr = input.value.split("/");
var dob = new Date();
dob.setFullYear(dobArr[2], dobArr[0]-1, dobArr[1]);
var date18 = new Date();
date18.setFullYear(dobArr[2]-18);
var date13 = new Date();
date13.setFullYear(dobArr[2]-13);
if (dob.valueOf() >= date18.valueOf()) {
//i'm at least 18
} else if (dob.valueOf() >= date13.valueOf()) {
//i'm at least 13 but not 18
} else {
//i'm less than 13
}
rezzif
2009-07-29 06:42:56
+5
A:
My first thought:
Usage:
var age = getAge(new Date(1984, 7, 31));
function getAge(birthDate) {
var now = new Date();
function isLeap(year) {
return (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0));
}
// days since the birthdate
var days = Math.floor((now.getTime() - birthDate.getTime())/1000/60/60/24);
var age = 0;
// iterate the years
for (var y = birthDate.getFullYear(); y <= now.getFullYear(); y++){
var daysInYear = isLeap(y) ? 366 : 365;
if (days >= daysInYear){
days -= daysInYear;
age++;
// increment the age only if there are available enough days for the year.
}
}
return age;
}
CMS
2009-07-29 06:49:47
given that you only want to find out if they're 18 i'd probably return once that point is reached so 90 year olds don't choke the system when they try to sign up :P
rezzif
2009-07-29 06:55:43
lol, I always think too generic...
CMS
2009-07-29 07:04:44
thanks for both of you! I've combined both of your code.
text
2009-07-29 08:02:10
You're welcome @text, glad to help...
CMS
2009-07-29 08:03:38
any idea how to integrate this on jquery plugin of http://bassistance.de/jquery-plugins/jquery-plugin-validation/
text
2009-07-29 09:05:19