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 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短