JS非空验证
<table class="tab-data" style=" margin:0px auto;">
<tr>
<td class="style2" > 姓<span style=" margin-left:50px;"></span>名:</td>
<td class="style3"> <div id="divStudentName"></div></td>
</tr>
<tr>
<td class="style2" >性<span style=" margin-left:50px;"></span>别:</td>
<td class="style3">
<select id="Sex" name="D1" class="input">
<option value="0" >>>请选择您的性别</option>
<option value="1">男</option>
<option value="2">女</option>
</select>
<div></div>//添加层
</td>
</tr>
<tr>
<td class="style2" > 年<span style=" margin-left:50px;"></span>龄:</td>
<td class="style3"><input id="age" type="text" class="input" />
<div></div>//添加层
</td>
</tr>
<tr>
<td class="style2" >学<span style=" margin-left:50px;"></span>校:</td>
<td class="style3">
<select id="school" name="D2" class="input">
<option value="0" selected="selected">>>请选择学校</option>
</select>
<div></div>//添加层
</td>
</tr>
<tr>
<td class="style2" > 年<span style=" margin-left:50px;"></span>级:</td>
<td class="style3">
<select id="seGrade" name="D3" class="input">
<option value="0" selected="selected">>>请选择年级</option>
</select>
<div></div>//添加层
</td>
</tr>
<tr>
<td class="style2" > 语文教材版本:</td>
<td class="style3">
<select id="ChineseVersion" name="D4" class="input">
<option value="0" selected="selected">>>请选择语文教材版本</option>
</select>
<div></div>//添加层
</td>
</tr>
<tr>
<td class="style2" > 数学教材版本:</td>
<td class="style3">
<select id="MathVersion" name="D5" class="input">
<option value="0" selected="selected">>>请选择数学教材版本</option>
</select>
<div></div>//添加层
</td>
</tr>
<tr>
<td class="style2" > 英语教材版本:</td>
<td class="style3">
<select id="EnglishVersion" name="D6" class="input">
<option value="0" selected="selected">>>请选择英语教材版本</option>
</select>
<div></div>//添加层
</td>
</tr>
<tr>
<td class="style2" >
</td>
<td class="style3">
<input id="btnUpdate" type="button" value="修改" />
</td>
</tr>
</table>
//验证输入
function validateInput(){
var result = false; //验证性别
if ($("#Sex").val() == "0") {
$("#Sex+div").empty();
$("#Sex+div").append("<span style=\" color:red;\">*请选择性别!</span>");
return false;
} else {
result = true;
}
//验证年龄
rgx = /^\s*$/
$("#age+div").empty();
if (rgx.test($("#age").val())) {
$("#age+div").append("<span id=\"ageerror\" style=\"color: #FF0000;\">*请输入年龄</span>");
result = false;
} else {
rgx = /^([1-9]|[1-9][0-9])$/;
if (!rgx.test($("#age").val())) {
$("#age+div").empty();
$("#age+div").append("<span id=\"ageerror\" style=\"color: #FF0000;\">*年龄只能输入1-99的数字</span>");
result = false;
}
}
//验证学校
$("#school+div").empty();
if ($("#school").val() == "" || $("#school").val() == "0") {
$("#school+div").append("<span id=\"schoolerror\" style=\"color: #FF0000;\">*请输入年龄</span>");
result = false;
}
else {
result = true;
}
//验证年级
$("#seGrade+div").empty();
if ($("#seGrade").val() == "" || $("#seGrade").val() == "0") {
$("#seGrade+div").append("<span id=\"seGradeerror\" style=\"color:#FF0000;\">*请输入年级</span>");
result = false;
}
else {
result = true;
} //验证语文教材版本是否已选择
if ($("#ChineseVersion").val() == "" || $("#ChineseVersion").val() == "0") {
$("#ChineseVersion+div").empty();
$("#ChineseVersion+div").append("<span id=\"chineseversionerror\" style=\"color:#FF0000;\">*请选择语文教材版本</span>");
return false;
} else {
result = true;
} //验证数学教材版本是否已选择
if ($("#MathVersion").val() == "" || $("#MathVersion").val() == "0") {
if ($("#MathVersion").val() == "" || $("#MathVersion").val() == "0") {
$("#MathVersion+div").empty();
$("#MathVersion+div").append("<span id=\"MathVersionError\" style=\"color:#FF0000;\">*请选择数学教材版本</span>");
return false;
} else { result = true;
} //验证英语教材版本是否已选择
if ($("#EngVersion").val() == "" || $("#EngVersion").val() == "0") {
$("#EngVersion+div").empty();
$("#EngVersion+div").append("<span id=\"EngVersionError\" style=\"color:#FF0000;\">*请选择英语教材版本</span>");
return false;
} else {
result = true;
} }
//保存修改信息
//保存修改信息
function SaveUPStudentInfo() {
if (!validateInput()) {
return false;
} else {
var para = "F=UpdateStudent_SaveInfo&pk_student=" + $("#hfStudentID").val() + "&sex=" + $("#Sex").val() + "&age=" + $("#age").val() + "&school=" + $("#school").val() + "&seGrade=" + $("#seGrade").val() + "&ChineseVersion=" + $("#ChineseVersion").val() + "&MathVersion=" + $("#MathVersion").val() + "&EnglishVersion=" + $("#EnglishVersion").val() + ""; top.returnserver(para); } }