试图验证我的表格

时间:2015-10-04 10:55:44

标签: javascript validation jsp

我正在尝试验证我在jsp中的表单,但它没有发生!!它运行正常,但我没有调用我的javascript文件中的函数validatemark

这是我的代码:

    <%@ 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>

这是我在ValidateMark.js中的javascript代码:

    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");
}

1 个答案:

答案 0 :(得分:0)

onsubmit="return(validatemark());"

onsubmit=" return validatemark();"