【问题标题】:Validate date for anyone over 18 with jQuery使用 jQuery 验证 18 岁以上的任何人的日期
【发布时间】:2013-09-23 10:26:22
【问题描述】:

我在我的网站上有一个表格,应该为 18 岁以上的人验证。

var day = $("#dobDay").val();
var month = $("#dobMonth").val();
var year = $("#dobYear").val();
var age = 18;
var mydate = new Date();
mydate.setFullYear(year, month-1, day);

var currdate = new Date();
currdate.setFullYear(currdate.getFullYear() - age);
var output = currdate - mydate
if ((currdate - mydate) > 0){
    // you are not 18
}

但它的工作方式完全相反。我希望 if 语句在用户未满 18 岁时采取行动。

提前感谢您的帮助

【问题讨论】:

  • 顺便说一句,请确保您也在服务器端执行此操作,因为绕过 JS 年龄检查器非常容易。
  • @RoryMcCrossan 是的.. 通过输入不同的日期:)
  • 嗯,是的,但我的意思是完全关闭 JS。如果这是一项法律要求,您真的不希望它那么容易通过。如果有人输入的日期不是他们的生日,那是他们的问题,而不是 OP。
  • 尝试设置 currdatemydate 一样通过提供年,月和日,也许它也考虑到时间,并保持该值currdate 总是大于 mydate

标签: javascript jquery validation


【解决方案1】:

检查这个DEMO

var day = 12;
var month = 12;
var year = 2006;
var age = 18;
var setDate = new Date(year + age, month - 1, day);
var currdate = new Date();

if (currdate >= setDate) {
  // you are above 18
  alert("above 18");
} else {
  alert("below 18");
}

【讨论】:

  • 此算法假设 2 月 29 日出生的人在 3 月 1 日满 18 岁,这适合某些司法管辖区,但不适用于其他司法管辖区。但否则它会起作用。
【解决方案2】:
var day = $("#dobDay").val();
var month = $("#dobMonth").val();
var year = $("#dobYear").val();
var age =  18;

var mydate = new Date();
mydate.setFullYear(year, month-1, day);

var currdate = new Date();
currdate.setFullYear(currdate.getFullYear() - age);

if(currdate < mydate)
{
    alert('You must be at least 18 years of age.');
}

【讨论】:

    【解决方案3】:

    这是我tested的稍微轻一点的版本:

    var day = 1;
    var month = 1;
    var year = 1999;
    var age = 18;
    
    var cutOffDate = new Date(year + age, month, day);
    
    if (cutOffDate > Date.now()) {
        $('output').val("Get Outta Here!");
    } else {
        $('output').val("Works for me!");
    }
    

    关键是将最小年龄添加到生日并确认它在当前日期之前。您正在检查当前日期减去最小年龄(基本上是允许的最新出生日期)是否大于提供的出生日期,这将为您提供相反的结果。

    【讨论】:

      【解决方案4】:

      使用 addMethod 函数的 jQuery Validator 插件的 18 年验证规则。

      jQuery.validator.addMethod(
              "validDOB",
              function(value, element) {              
                  var from = value.split(" "); // DD MM YYYY
                  // var from = value.split("/"); // DD/MM/YYYY
      
                  var day = from[0];
                  var month = from[1];
                  var year = from[2];
                  var age = 18;
      
                  var mydate = new Date();
                  mydate.setFullYear(year, month-1, day);
      
                  var currdate = new Date();
                  var setDate = new Date();
      
                  setDate.setFullYear(mydate.getFullYear() + age, month-1, day);
      
                  if ((currdate - setDate) > 0){
                      return true;
                  }else{
                      return false;
                  }
              },
              "Sorry, you must be 18 years of age to apply"
          );
      

      $('#myForm')
              .validate({
                  rules : {
                      myDOB : {
                          validDOB : true
                      }
                  }
              });
      

      【讨论】:

      • 通过更改以下内容来工作:var from = value.split("-");
      【解决方案5】:

      如果它的工作方式相反,您是否尝试将倒数第二行的&gt; 换成&lt;

      【讨论】:

      • 我有,然后它会验证一切。
      【解决方案6】:

      我认为如果我们重命名变量会更容易理解

      我的日期 => 给定日期
      currdate => 阈值日期

      如果 givenDate > thresholdDate => 你不是 18 岁
      否则 => 你 18 岁

      if ( givenDate > thresholdDate ){
          // you are not 18
      }
      

      if ((givenDate - thresholdDate) > 0){
          // you are not 18
      }
      

      if ((mydate - currdate ) > 0){
          // you are not 18
      }
      

      【讨论】:

        猜你喜欢
        • 2018-11-17
        • 1970-01-01
        • 2018-06-27
        • 1970-01-01
        • 2021-12-22
        • 2014-12-24
        • 1970-01-01
        • 2010-12-21
        • 2022-07-01
        相关资源
        最近更新 更多