使用 jQuery 验证 18 岁以上任何人的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18956956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validate date for anyone over 18 with jQuery
提问by Dom
I have a form on my site that should validate for anyone who is over 18.
我的网站上有一个表格,可以验证任何 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
}
But it working totally opposite way. I would like the if statement to take action when user is over under 18 years old.
但它的工作方式完全相反。我希望 if 语句在用户 18 岁以下时采取行动。
Thank you for your help in advance
提前谢谢你的帮助
回答by developerCK
回答by Ankit Agrawal
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.');
}
回答by Anthony
Here is a somewhat lighter version that I 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!");
}
The key is to add the minimum age to the birthdate and confirm that it is before the current date. You are checking if the current date minus the minimum age (basically the latest birthdate allowed) was greater than than the birthdate provided, which will give you the reverse.
关键是将最小年龄添加到出生日期并确认它在当前日期之前。您正在检查当前日期减去最小年龄(基本上是允许的最晚出生日期)是否大于提供的出生日期,这会给您相反的结果。
回答by Rijo
18 year old validation rule for jQuery Validator plugin using addMethodfunction.
使用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"
);
and
和
$('#myForm')
.validate({
rules : {
myDOB : {
validDOB : true
}
}
});
回答by nilgun
I think it will be easier to understand if we rename the variables
我认为如果我们重命名变量会更容易理解
mydate => givenDate
currdate => thresholdDate
mydate => givenDate
currdate => thresholdDate
if givenDate > thresholdDate => you are not 18
else => you are 18
如果 givenDate > thresholdDate => 你不是 18
else => 你是 18
i.e.
IE
if ( givenDate > thresholdDate ){
// you are not 18
}
i.e
IE
if ((givenDate - thresholdDate) > 0){
// you are not 18
}
i.e.
IE
if ((mydate - currdate ) > 0){
// you are not 18
}
回答by Calvin
if it's working the opposite way have you tried swapping the >
for a <
on the second to last line?
如果它以相反的方式工作,您是否尝试在倒数第二行交换>
a <
?