1

大家好,在我们的 jsp 页面上进行一些表单验证。

当您在容量字段中输入任何值或非数字值时,我会出现警告框。但是点击“确定”后,页面被重定向到一个错误:

HTTP Status 500 - For input string: ""

或在容量字段中输入“a”时

HTTP Status 500 - For input string: "a"

我将如何重新加载页面而不是将我带到错误页面?

我已经尝试了许多内置的 Javascript 函数,但仍然指向错误页面,例如 location.reload() 等。

<%@ 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>Mars University Lab System</title>
    <link rel="stylesheet" href="style.css" type="text/css" media="screen">
</head>

<body>
<jsp:include page="headerLecturer.jsp"/>
<script>

function chkNumeric(strString)
                   //  check for valid numeric strings  
                   {
                       var strValidChars = "0123456789.-";
                       var strChar;
                       var blnResult = true;

                       if (strString.length == 0) return false;

                       //  test strString consists of valid characters listed above
                       for (i = 0; i < strString.length && blnResult == true; i++)
                          {
                                strChar = strString.charAt(i);
                                if (strValidChars.indexOf(strChar) == -1)
                             {
                             blnResult = false;
                             }
                          }
                       return blnResult;
                   }


                 function validateForm()
                 {
                    var capacity=document.forms["createLabs"]["capacity"].value;
                    var day=document.forms["createLabs"]["day"].value;
                    var time=document.forms["createLabs"]["time"].value;
                    var room=document.forms["createLabs"]["room"].value;

                    if (capacity==null || capacity=="")
                      {
                            alert("Capacity must be filled out");
                            document.createLabs.capacity.focus();
                            return false;
                      }
                    else if(day==null || day=="")
                     {
                            alert("Day must be filled out");
                            document.createLabs.day.focus();
                            return false;
                     }
                     else if(time==null || time=="")
                     {
                            alert("Time must be filled out");
                            document.createLabs.time.focus();
                            return false;
                     }
                     else if(room==null || room=="")
                     {
                            alert("Room must be filled out");
                            document.createLabs.room.focus();
                            return false;

                    }
                    else if (chkNumeric(document.createLabs.capacity.value) == false) 
                    {
                            alert("Please check - non numeric value!");
                            document.createLabs.room.focus();
                            return false;

                    }
                    else if (chkNumeric(document.createLabs.room.value) == false) 
                    {
                            alert("Please check - non numeric value!");
                            document.createLabs.room.focus();
                            return false;
                    }




                            var link2 = document.getElementById('submit');

                                link2.onclick = function ()     
                                {
                                    if( confirm("Please fill in all the required fields") )
                                    {
                                        window.location.reload();
                                    }
                                    return false;
                                }


}


</script>

<tr>
<td>
</td>
</tr>

<tr>
<td>
<div id = "centrecontent">
<br>
<h3>Create Labs</h3>
<%String[] list1 = (String[])request.getAttribute("res1"); %>
<%String[] list2 = (String[])request.getAttribute("res2"); %>


        <form name ="createLabs" ACTION="CreateLab" method="post">

        Subject: <select name="subject_id">
        <%
        for(int i=0; i<list1.length; i++)  
        {
            //for(int m=0; m<list2.length;i++)
            //{
                out.println("<option value="+list1[i]+"> "+list2[i]+" </option>");

        //}
            out.println("list1:"+list1[0]);
            }
            %>
        </select><br><br>
        Capacity: <input type="text" name="capacity"/><br /><br>
        <input type="hidden" name="spots_left"/>
        Day: <input type="text" name="day"/><br /><br>
        Time (HH:MM:SS): <input type="time" name="time"/><br /><br>
        Room: <input type="text" name="room"/><br /><br>
        <input type=SUBMIT value="Submit" name="Submit" onClick="validateForm()" />

    </form>
</div>

<jsp:include page="footer.jsp"/>


</body>

</html>
4

1 回答 1

0

问题来自服务器端,所以问题在于javascript函数中的验证

我更改了 validateForm 但没有检查。

也许会有所帮助

      function validateForm()
         {
            var capacity=document.forms["createLabs"]["capacity"].value;
            var day=document.forms["createLabs"]["day"].value;
            var time=document.forms["createLabs"]["time"].value;
            var room=document.forms["createLabs"]["room"].value;

            if (capacity === undefined)
              {
                    alert("Capacity must be filled out");
                    document.createLabs.capacity.focus();
                    return false;
              }

                if(day === undefined)
             {
                    alert("Day must be filled out");
                    document.createLabs.day.focus();
                    return false;
             }

                 if(time === undefined)

             {
                    alert("Time must be filled out");
                    document.createLabs.time.focus();
                    return false;
             }

                 if(room === undefined)

             {
                    alert("Room must be filled out");
                    document.createLabs.room.focus();
                    return false;

            }

                if ( isNaN(capacity))
                {
                     alert("Please check - non numeric value!");
                    document.createLabs.room.focus();
                    return false;
                }

                    if ( isNaN(room))
                {
                     alert("Please check - non numeric value!");
                    document.createLabs.room.focus();
                    return false;
                }

                if(room === undefined)
            {
                    alert("Please check - non numeric value!");
                    document.createLabs.room.focus();
                    return false;
            }




                    var link2 = document.getElementById('submit');

                        link2.onclick = function ()     
                        {
                            if( confirm("Please fill in all the required fields") )
                            {
                                window.location.reload();
                            }
                            return false;
                        }


                            }

此外,您需要为“onsubmit”事件放置 validateForm 触发器

<form name ="createLabs" ACTION="CreateLab" method="post" onsubmit="return validateForm()">

        Subject: <select name="subject_id">
        <%
        for(int i=0; i<list1.length; i++)  
        {
            //for(int m=0; m<list2.length;i++)
            //{
                out.println("<option value="+list1[i]+"> "+list2[i]+" </option>");

        //}
            out.println("list1:"+list1[0]);
            }
            %>
        </select><br><br>
        Capacity: <input type="text" name="capacity"/><br /><br>
        <input type="hidden" name="spots_left"/>
        Day: <input type="text" name="day"/><br /><br>
        Time (HH:MM:SS): <input type="time" name="time"/><br /><br>
        Room: <input type="text" name="room"/><br /><br>
        <input type="submit" value="Submit" name="Submit"   />

    </form>
于 2012-10-08T08:17:43.227 回答