<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title></title> | |
<script> | |
// | |
function sname(){ | |
if( document.getElementById("name1").value.length == 0){ | |
alert("不能为空!"); | |
return false; | |
}else{ | |
return true; | |
} | |
} | |
function spass(){ | |
if(document.getElementById("pass").value.length < 6){ | |
alert("不能小于6位"); | |
return false; | |
}else{ | |
return true; | |
} | |
} | |
function spass1(){ | |
if(document.getElementById("pass1").value!=document.getElementById("pass").value){ | |
alert("两次密码不一样"); | |
return false; | |
}else{ | |
return true; | |
} | |
} | |
function sage(){ | |
if(document.getElementById("age").value <= 18){ | |
alert("年龄必须大于18!"); | |
return false; | |
}else{ | |
return true; | |
} | |
} | |
function zc(){ | |
if(document.getElementById("name1").value.length == 0){ | |
alert("用户名不能为空!"); | |
return; | |
} | |
if(document.getElementById("pass").value.length < 6){ | |
alert("密码输入不合法"); | |
return; | |
} | |
if(document.getElementById("pass1").value!=document.getElementById("pass").value){ | |
alert("两次密码不一致"); | |
return; | |
} | |
//验证年龄 | |
if(document.getElementById("age").value <= 18){ | |
alert("年龄不合法"); | |
return; | |
} | |
//验证爱好是否勾选 | |
var bobys = document.getElementsByName("boby"); | |
var flag = false; | |
for(var i=0;i<bobys.length;i++){ | |
if(bobys[i].checked){ | |
flag = true; | |
break; | |
} | |
} | |
if(!flag){ | |
alert("爱好最少选择一项"); | |
return ; | |
} | |
alert("每一项都合法,可以跳转了"); | |
//提交form表单 | |
//document.getElementById("form1").submit(); | |
window.location = "http://www.baidu.com"; | |
} | |
// function szx(){ | |
// var selectzx = document.getElementById("zx"); | |
// var activezx = selectzx.options[selectzx.selectedIndex].value; | |
// if(activezx=="请选择"){ | |
// alert("请选择直辖市"); | |
// selectzx.focus(); | |
// return false; | |
// } | |
// } | |
</script> | |
</head> | |
<body> | |
<center> | |
<form id="form1" action="http://www.baidu.com"> | |
用户名:<input type="text" id="name1" onblur="sname()"/><br /><br /> | |
密码:<input type="password" id="pass" onblur="spass()"/><br /><br /> | |
确定密码:<input type="password" id="pass1" onblur="spass1()"/><br /><br /> | |
密码提示问题:<select> | |
<option>请选择一个问题</option> | |
<option>您今年年龄多大了?</option> | |
<option>您小学班主任的姓名?</option> | |
<option>您最好的朋友的手机号?</option> | |
</select><br /><br /> | |
密码提示答案:<input type="text" id="daan" /><br /><br /> | |
性别:<input type="radio" name="sex" id="nan" checked="checked"/>男<input type="radio" name="sex" id="nv" />女<br /><br /> | |
年龄:<input type="text" id="age" onblur="sage()"/><br /><br /> | |
籍贯: <select id="zx" onchange="szx()"> | |
<option value="0">请选择</option> | |
<option value="1">北京</option> | |
<option value="2">黑龙江</option> | |
<option value="3">辽宁</option> | |
</select> 省/直辖市 | |
<select> | |
<option>请选择</option> | |
<option>北京</option> | |
<option>齐齐哈尔</option> | |
<option>沈阳</option> | |
</select> 市<br /><br /> | |
爱好:<input type="checkbox" name="boby" id="sw" />上网<input type="checkbox" name="boby" id="xj"/>学JAVA<input type="checkbox" name="boby" id="sj" />睡觉<br /><br /> | |
个人介绍:<textarea></textarea><br /><br /> | |
上传头像:<input type="file" id="tx" /><br /><br /> | |
<input type="button" value="注册" onclick="zc()"/> | |
</form> | |
</center> | |
</body> | |
</html> | |
表单验证
最新推荐文章于 2023-03-16 10:17:29 发布