admin管理员组文章数量:1305084
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.
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.
Share Improve this question asked Jul 29, 2009 at 6:21 texttext 3631 gold badge9 silver badges19 bronze badges2 Answers
Reset to default 7My 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;
}
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
}
本文标签: javascripthow to check the age of registrant during registration using jqueryStack Overflow
版权声明:本文标题:javascript - how to check the age of registrant during registration using jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741794954a2397884.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论