Javascript - 年龄验证

时间:2013-06-05 02:06:27

标签: javascript verification

嘿javascript大师,

尝试为客户的网站创建年龄验证页面。下面的代码无法正常运行,因为无论您选择哪一年,它仍然允许您进入该网站。不确定我应该注意什么来纠正。

感谢任何帮助。

<script type="text/javascript"><!--
function checkAge(f){
var dob=new Date();
var date=dob.getDate();
var month=dob.getMonth() + 1;
var year=dob.getFullYear();
var cmbmonth=parseInt(document.getElementById("cmbmonth").options[document.getElementById("cmbmonth").selectedIndex].value);
var cmbday=parseInt(document.getElementById("cmbday").options[document.getElementById("cmbday").selectedIndex].value);
var cmbyear=parseInt(document.getElementById("cmbyear").options[document.getElementById("cmbyear").selectedIndex].value);

age=year-cmbyear;

if(cmbmonth>month){age--;}
else{if(cmbmonth==month && cmbday>=date){age--;}}

if(cmbmonth==0){alert("You must enter the month you were born in.");return false;}
else if(cmbday==0){alert("You must enter the day you were born on.");return false;}
else if(cmbyear==2005){alert("You must enter the year you were born in.");return false;}
else if(age<13){alert("You are unable to view this site!");location.replace("http://www.dharmatalks.org");return false;}
else{return true;}

}

// --></script>

1 个答案:

答案 0 :(得分:0)

由于月份和年份的长度不同,计算年龄,月份和日期的年龄比应该的要复杂一些。这是一个函数,它将返回两个日期之间的差异,包括年,月,日,小时,分钟和秒。

function dateDifference(start, end) {

  // Copy date objects so don't modify originals
  var s = new Date(+start);
  var e = new Date(+end);
  var timeDiff, years, months, days, hours, minutes, seconds;

  // Get estimate of year difference
  years = e.getFullYear() - s.getFullYear();

  // Add difference to start, if greater than end, remove one year
  // Note start from restored start date as adding and subtracting years
  // may not be symetric
  s.setFullYear(s.getFullYear() + years);
  if (s > e) {
    --years;
    s = new Date(+start);
    s.setFullYear(s.getFullYear() + years);
  }
  // Get estimate of months
  months = e.getMonth() - s.getMonth();
  months += months < 0? 12 : 0;

  // Add difference to start, adjust if greater
  s.setMonth(s.getMonth() + months);
  if (s > e) {
    --months;
    s = new Date(+start);
    s.setFullYear(s.getFullYear() + years);
    s.setMonth(s.getMonth() + months);
  }


  // Get remaining time difference, round to next full second
  timeDiff = (e - s + 999) / 1e3 | 0;
  days     =  timeDiff / 8.64e4 | 0;
  hours    = (timeDiff % 8.64e4) / 3.6e3 | 0;
  minutes  = (timeDiff % 3.6e3) / 6e1 | 0;
  seconds  =  timeDiff % 6e1;

  return [years, months, days, hours, minutes, seconds];
}

您可以在年份部分之后缩小上述内容,如果需要,只返回。

请注意,在您的代码中:

var cmbmonth=parseInt(document.getElementById("cmbmonth").options[document.getElementById("cmbmonth").selectedIndex].value);

可以是:

var cmbmonth = document.getElementById("cmbmonth").value;

不需要 parseInt ,Date构造函数将很乐意使用字符串值。如果您使用了日历月号来表示值(即Jan = 1),那么在将其提供给Date构造函数之前减去1,但更简单的是使用javascript月份索引来表示值(即Jan = 0)。

然后你可以这样做:

var diff = dateDifference(new Date(cmbyear, cmbmonth, cmbdate), new Date());
if (diff[0] < 18) {
  // sorry, under 18
}