jQuery实现验证年龄简单思路
这段代码假定环境是一个ID为age-form的”form”,三个ID分别为”day”,”month”,”year”。
$("#age-form").submit(function(){
varday=$("#day").val();
varmonth=$("#month").val();
varyear=$("#year").val();
varage=18;
varmydate=newDate();
mydate.setFullYear(year,month-1,day);
varcurrdate=newDate();
currdate.setFullYear(currdate.getFullYear()-age);
if((currdate-mydate)<0){
alert("Sorry,onlypersonsovertheageof"+age+"mayenterthissite");
returnfalse;
}
returntrue;
});
也许你想使用一个比alert更优雅的提示方法。并且应该在服务器端进行再次验证,不然只能在启用js的客户端验证。
反正代码的意思就是让用户填出生年月,然后根据当前时间计算是否小于网站要求的年龄,小于就提示。
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语