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 入住酒店文案祝福语简短