这是第一次在这里发帖提问。我正在使用年龄验证页面,其中包含出生日期下拉菜单和提交按钮。除了以下一个问题外,它的效果很好。
用户选择年龄低于规定年龄且未通过年龄门后,此用户仍可返回年龄检查页面尝试选择较旧的出生日期,这样就可以让用户成功通过年龄门。我想阻止已经输入无限制出生日期的用户返回并执行此操作。我假设它是某种类型的cookie设置。但是,我正在努力解决这个问题,并且仍然使用javascript初学者,所以任何帮助或建议将不胜感激。
来自https://github.com/tmock12
var ageCheck = {
//Set the minimum age and where to redirect to
minimumAge : 17,
userIsOldEnoughPage : "index.html",
userNotOldEnoughPage : "age_unauthorized.html",
start: function() {
this.setUsersBirthday();
if (this.userIsOverMinimumAge()) {
this.setCookie("usersBirthday",this.usersBirthday,30);
window.location = this.userIsOldEnoughPage;
} else{
this.notMinimumAge();
};
},
usersBirthday : new Date(),
setTheMonth : function() {
var selectedMonth = document.getElementById("month").value;
if (selectedMonth === "1") {
this.setDaysForMonth(29);
}
else if (selectedMonth === "3" ||
selectedMonth === "5" ||
selectedMonth === "8" ||
selectedMonth === "10") {
this.setDaysForMonth(30);
}
else {
this.setDaysForMonth(31);
};
},
setDaysForMonth : function(x) {
var daySelectTag = document.getElementsByName('day')[0];
daySelectTag.options.length = 0;
for(var i=1; i <= x; i++) {
daySelectTag.options.add(new Option(i, i));
}
},
setUsersBirthday: function() {
var usersMonth = document.getElementById("month").value;
var usersDay = document.getElementById("day").value;
var usersYear = document.getElementById("year").value;
this.usersBirthday.setMonth(usersMonth);
this.usersBirthday.setDate(usersDay);
this.usersBirthday.setFullYear(usersYear);
},
setMinimumAge: function() {
var today = new Date();
today.setFullYear(today.getFullYear() - this.minimumAge);
return today;
},
notMinimumAge : function() {
window.location = this.userNotOldEnoughPage
},
userIsOverMinimumAge: function () {
if (this.usersBirthday < this.setMinimumAge()) {
return true;
}
},
setCookie: function (c_name,value,exMinutes) {
var exdate=new Date();
exdate.setMinutes(exdate.getMinutes() + exMinutes);
var c_value=escape(value) + ((exMinutes==null) ? "" : ";
expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
},
getCookie: function (c_name) {
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(x);
}
}
},
checkCookie: function () {
var usersBirthday=this.getCookie("usersBirthday");
if (usersBirthday==null || usersBirthday=="") {
window.location = "agegate.html";
}
}
}