I have been attempting to validate my form in JSP, but unfortunately it is not working as expected. The form runs fine, however, the function 'validatemark' from my JavaScript file is not being called.
Below is a snippet of my code:
<%@ page import = "javax.servlet.http.HttpServlet" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import= "org.akash.java.DabManager" %>
<%@ page import= "org.akash.java.GetsSets" %>
<%@ page import= "java.sql.ResultSet" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="ValidateMark.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>FILL IN YOUR MARKS HERE</title>
</head>
<body>
<form action="MarksServlet" method="post" name="SubjectMarks" onsubmit="return(validatemark());" >
<table cellpadding="2" width="20%" bgcolor="maroon" align="center"
cellspacing="2">
<tr>
<td colspan=2>
<center><font size=4><b>Subject Marks Form</b></font></center>
</td>
</tr>
<%
String stid= request.getParameter("identity");
%>
</tr>
<tr>
<td>ID</td>
<td><input type="text" name="iden" id="iden" size="10" value= <%=stid %>></td>
</tr>
<tr>
<td>Name</td>
<%
try {
ResultSet resu= null;
DabManager dab= new DabManager();
resu= dab.Getsname(stid);
if(resu.next())
{
String sname= resu.getString("sname");
%>
<td><input type="text" name="yourname" id="yourname" size="30" value=<%=sname %>></td>
<%
}
}catch(Exception e)
{
e.printStackTrace();
}
%>
</tr>
<tr>
<td>Physics</td>
<td><input type="text" name="Physics" id="Physics" size="10"></td>
</tr>
<tr>
<td>Chemistry</td>
<td><input type="text" name="Chemistry" id="Chemistry" size="10"></td>
</tr>
<tr>
<td>Mathematics</td>
<td><input type="text" name="Maths" id="Maths" size="10"></td>
</tr>
<tr>
<td>Biology</td>
<td><input type="text" name="Biology" id="Biology" size="10"></td>
</tr>
<tr>
<td>
<input type="reset">
</td>
<td colspan="2">
<input type="submit" value="Submit Form" onclick ="confirms()"/>
</td>
</tr>
</table>
</form>
</body>
</html>
And here is my JavaScript code in ValidateMark.js:
function validatemark()
{
if( document.SubjectMarks.iden.value == "" || isNaN( document.SubjectMarks.iden.value))
{
alert( "Please provide a valid id!" );
document.SubjectMarks.iden.focus() ;
return false;
}
if( document.SubjectMarks.yourname.value == "" )
{
alert( "Please provide your Name!" );
document.SubjectMarks.yourname.focus() ;
return false;
}
if( document.SubjectMarks.Physics.value == "" || isNaN(document.SubjectMarks.Physics.value)|| document.SubjectMarks.Physics.value >100)
{
alert( "Please enter valid marks!!" );
document.SubjectMarks.Physics.focus() ;
return false;
}
if( document.SubjectMarks.Chemistry.value == "" || isNaN(document.SubjectMarks.Chemistry.value)|| document.SubjectMarks.Chemistry.value >100)
{
alert( "Please enter valid marks!!" );
document.SubjectMarks.Chemistry.focus() ;
return false;
}
if( document.SubjectMarks.Maths.value == "" || isNaN(document.SubjectMarks.Maths.value)|| document.SubjectMarks.Maths.value >100)
{
alert( "Please enter valid marks!!" );
document.SubjectMarks.Maths.focus() ;
return false;
}
if( document.SubjectMarks.Biology.value == "" || isNaN(document.SubjectMarks.Biology.value)|| document.SubjectMarks.Biology.value >100)
{
alert( "Please enter valid marks!!" );
document.SubjectMarks.Physics.focus() ;
return false;
}
return true;
}
function confirms()
{
if(validatemark()== true)
alert("Marks Entered Successfully");
}