-1
我只是想做一個簡單的網絡應用程序。它應該採取所有輸入,驗證它們並將其存儲在數據庫中或顯示錯誤。我在通過外部JavaScript驗證表單時遇到了問題。如果一個字段爲空,則所有檢查都完成,但JSP將控制指向Servlet。我需要停止JSP頁面繼續前進。請幫忙!下面是代碼:如何阻止JSP頁面執行servlet?
JSP:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
<script type="text/javascript" src="js/validationJS.js"></script>
</head>
<body>
<h1 align="center">Teacher Management System</h1>
<form name="TeacherRegistrationForm" action="TeacherController" method="post" >
<input type="hidden" name="source" value="AddTeacher">
<table>
<tr>
<td>
ID:<input type="text" name="teacherId" />
</td>
</tr>
<tr>
<td>
Name:<input type="text" name="teacherName" />
</td>
</tr>
<tr>
<td>
Address:<textarea type="text" rows="5" name="teacherAddress" ></textarea>
</td>
</tr>
<tr>
<td>
Qualification:<input type="text" name="teacherQualification" />
</td>
</tr>
<tr>
<td>
Years Of Experience:<input type="text" name="teacherExp" />
</td>
</tr>
<tr>
<td>
Age:<input type="text" name="teacherAge" />
</td>
</tr>
<tr>
<td>
<input type="submit" name="teacherAdd" value="ADD" onclick="validate()"/>
<input type="reset" name="teacherReset" value="RESET FIELDS" /><br><br>
<a href="WelcomePage.jsp" style="text-decoration: underline">Go Back</a>
</td>
</tr>
</table>
</form>
</body>
</html>
的Javascript:
function validate()
{
boolean result=false;
if(document.TeacherRegistrationForm.teacherId.value=="")
{
document.TeacherRegistrationForm.teacherId.value= "Please Enter a valid ID";
document.TeacherRegistrationForm.teacherId.focus();
alert("Please enter valid ID");
result=false;
}else{result=true;}
return result;
}