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 badges
Add a ment  | 

2 Answers 2

Reset to default 7

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;
}

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