如何判断年龄大于18岁

1.jquery的实现方式:

下面这个:

(1)var birth = document.getElementById("dateBirth").children[1].value;
     它是用来获取struts2.0的日期控件,输入日期然后获取输入值的方式。

(2)var agree = document.signupFormSubmit.ch1Agree.checked;
    它是用来获取chekbox复选框,是否被选中。如果选中则为true,否则是false。

<script type='text/javascript'>
	$(function(){
		$("#signupFormSubmit").submit(function(){
			var agree = document.signupFormSubmit.ch1Agree.checked;
			var birth = document.getElementById("dateBirth").children[1].value;
			var myDate = new Date();
			var str = birth.split("/");
			if(myDate.getFullYear() - str[2] > 18){
				if(agree == false){
					alert("You have to read and agree terms of use to sign up");
					return false;
				}
			}else if(myDate.getFullYear() - str[2] == 18){
				if(myDate.getMonth()+1 - str[0] > 0){
					if(agree == false){
						alert("You have to read and agree terms of use to sign up");
						return false;
					}
				}else if(myDate.getMonth()+1 - str[0] == 0){
					if(myDate.getDate() - str[1] >= 0){
						if(agree == false){
							alert("You have to read and agree terms of use to sign up");
							return false;
						}
					}else{
						alert("your age less then 18");
						return false;
					}
				}else{
					alert("your age less then 18");
					return false;
				}
			}else{
				alert("your age less then 18");
				return false;
			}
		});
})
</script>

 

2.javascript的判断方式:

function checkDateBirth(){
	var agree = document.signupFormSubmit.ch1Agree.checked;
	var birth = document.getElementById("dateBirth").children[1].value;
	var myDate = new Date();
	var str = birth.split("/");
	if(myDate.getFullYear() - str[2] > 18){
		alert(">18");
		if(agree == true){
			alert(">18 ==true tijiao");
			document.getElementById("signupFormSubmit").submit(); 
			return true;
		}else{
			alert(">18 ==false ?");
			alert("you should agree with the terms");
			return false;
		}
	}else if(myDate.getFullYear() - str[2] == 18){
		alert("==18");
		if(myDate.getMonth()+1 - str[0] > 0){
			alert(">3");
			if(agree == true){
				alert(">3 ==true tijiao");
				document.getElementById("signupFormSubmit").submit(); 
				return true;
			}else{
				alert(">3 ==false ?");
				alert("you should agree with the terms");
				return false;
			}
		}else if(myDate.getMonth()+1 - str[0] == 0){
			alert("==3");
			if(myDate.getDate() - str[1] >= 0){
				alert(">=14");
				if(agree == true){
					alert(">=14 ==true tijiao");
					document.getElementById("signupFormSubmit").submit(); 
					return true;
				}else{
					alert(">=14 ==false");
					alert("you should agree with the terms");
					return false;
				}
			}else{
				alert("<14");
				alert("your age less then 18");
				return false;
			}
		}else{
			alert("<3");
			alert("your age less then 18");
			return false;
		}
	}else{
		alert("<18");
		alert("your age less then 18");
		return false;
	}
}

 

你可能感兴趣的:(JavaScript,jquery,UP)