前台页面JS验证文本框非空,数值范围选择,单选框必选,复选框必选
<script type="text/javascript">
function check(){
var xm = document.getElementById("xm").value;
if(xm == null || xm == ''){
alert("用户名不能为空");
return false;
}
if(!document.getElementById("male").checked&&!document.getElementById("female").checked ){
alert("性别不能为空");
return false;
}
var csrq = document.getElementById("csrq").value;
if(csrq == null || csrq == ''){
alert("出生日期不能为空");
return false;
}
var fs =document.getElementById("fs").value;
if(fs == null || fs == ''){
alert("分数不能为空");
return false;
}
if(parseInt(fs) < 0||parseInt(fs)>999) {
alert("数值范围不对");
return false;
}
var names = document.getElementsByName("aihao");
var flag = false ;
for(var i=0;i<names.length;i++){
if(names[i].checked){
flag = true ;
break ;
}
}
if(!flag){
alert("请最少选择一项!");
return false ;
}
var slt=document.getElementById("liebiao");
if(slt.value==""){
alert("请选择一个项目");
return false;
}
return true;
}
</script>
</head>
<body>
<form action="${pageContext.request.contextPath }/InsertStuServlet" method="get" onsubmit="return check()">
姓名<input name="xm" id="xm" type="text" /><br />
性别<input name="xb" id="male" type="radio" value="男" />男
<input name="xb" id="female" type="radio" value="女" />女<br />
出生日期<input name="csrq" id="csrq" type="text" class="Wdate" onClick="WdatePicker()" /><br />
分数<input name="fs" id="fs" type="text" /><br />
爱好:
<input name="aihao" type="checkbox" value="爱好1" />爱好1;
<input name="aihao" type="checkbox" value="爱好2" />爱好2;
<input name="aihao" type="checkbox" value="爱好3" />爱好3;<br>
选择:
<select name="liebiao" id="liebiao">
<option value ="">-请选择-</option>
<option value ="volvo">Volvo</option>
<option value ="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<br>
<input name="" type="submit" value="提交" />
</form>
</body>