年龄验证 - 关闭对话窗口

时间:2013-06-06 15:26:03

标签: javascript jquery-ui verification gate

使用Nation Builder作为客户网站的平台,需要创建年龄门。我会在一个单独的页面上进行年龄验证,但显然它不适用于这个平台,因此我使用Jquery对话框显示在主页的顶部。我验证年龄的代码工作,但需要调整它以关闭窗口小部件UI而不是URL重定向。

我不是编程专家,所以任何帮助和任何帮助都会受到赞赏。这是一个链接。 http://patricialourenco.com/test.html 干杯!

var ageCheck = {

  //Set the minimum age and where to redirect to
  minimumAge : 13,
  userIsOldEnoughPage : "http://www.bullyproject.com",
  userNotOldEnoughPage : " http://www.pacerkidsagainstbullying.org/#/home",


  //Leave this stuff alone please :)

  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 = "ageCheckTester.html";
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找.close方法 - 在用户按下按钮提交其年龄后,您将如何使用它:

$('#dialog-modal').dialog('close'); //hides modal, shows content behind

有关jQuery UI对话框的更多文档:

http://api.jqueryui.com/dialog/#method-close


编辑:以下是如何更新start()方法:

 start: function() {
   this.setUsersBirthday();
   if (this.userIsOverMinimumAge()) {
     this.setCookie("usersBirthday",this.usersBirthday,30);
     $('#dialog-modal').dialog('close');
   } else{
     this.notMinimumAge();
   };
 }, 

和JSFiddle演示:http://jsfiddle.net/donmccurdy/czERa/


编辑#2 - (回应OP关于检查每页上的cookie的问题)

您在检查Cookie的代码中有一些错误。我已经更新了JSFiddle,这里有一个错误的快速版本:

1)代替您当前拥有的始终显示对话框的代码,首先检查cookie。像这样:

$(function() {
    window.ageCheck.checkCookie();
});

2)更新checkCookie()方法以在必要时显示对话框:

checkCookie:   function () {
  var usersBirthday=this.getCookie("usersBirthday");
  if (!usersBirthday) {
     // unknown age - show dialog.
     $( "#dialog-modal" ).dialog({modal: true});
     return;
  }
  this.usersBirthday = new Date(usersBirthday);
  if (this.userIsOverMinimumAge()) {
     $('#dialog-modal').hide();
     // user is > 13, don't show dialog.
 } else {
    // user is < 13, redirect to another page.
    this.notMinimumAge(); 
 }
}

3)你的getCookie方法有一个错误,因为你正在返回&#34; usersBirthday&#34;而不是生日本身。修正了这里:

getCookie: function (c_name) {
  var i,x,y,ARRcookies=document.cookie.split(";");
  for (i=0;i<ARRcookies.length;i++)
  {
    //split on '=', then check cookie
    x=ARRcookies[i].split('=');
    if (x[0]===c_name){
      return unescape(x[1]);
    }
  }
},