0

我已经编写了 JSP 代码,我需要检查空格验证,但是它不起作用,谁能告诉我原因。单击提交按钮时,它必须验证空格和字符。

JSP 代码

<%-- 
    Document   : LapsePeriodicFeedBack
    Created on : Dec 7, 2012, 2:50:28 PM
    Author     : Admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.util.*" %>
<%@page import="PeriodicFeedBack.PeriodicType"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>LapsePeriodicList</title>
        <script>
            function validateForm(){

                var selectindex=document.forms["lapse"]["periodmenu"].selectedIndex;
                var whitespace = new RegExp("^\w*$");
                var x= document.forms["lapse"]["lapsedays"].value;
                var textlength=document.forms["lapse"]["lapsedays"].length;
                if(x==""){
                    alert("Days Cant Be Empty");
                    return false;
                }
                if(x==whitespace){
                    alert("White Spaces are not Allowed");
                    return false;
                }
                if(selectindex==0){

                    alert("Please select Days from menu");
                }


            }
            if(x=="/\\s"){
                    alert('Sorry, you are not allowed to enter any spaces');
                    x.value=x.value.replace(/\s/g,'');
                    }

        </script>

    </head>
    <body>
        <form  method="post" name="lapse" onsubmit="return validateForm()">
            <table width="500px" border="1" align="center" style='border-collapse: collapse;font: 13px Arial, Helvetica, sans-serif;' bordercolor='#000000'>

                <tr  style="color:'#ffffff';background-color:#CA1619;background-repeat:repeat-x;font: 13px Arial, Helvetica, sans-serif;">
                    <td colspan="4" align="center">
                <font color="#ffffff" > <b>Periodic Feedback List</b></font></td></tr>
                <tr>
                    <td align="center">
                    Periodic Feedback Days</td>
                    <td align="center">
                        <select id="periodmenu">
                            <option> Select</option>
                            <%
        PeriodicType p = new PeriodicType();
        ArrayList periodicList = p.getPeriodicType();
        for (int i = 0; i < periodicList.size(); i++) {%>
                            <option>

                                <% out.println(periodicList.get(i));
        }%>
                            </option>
                    </select></td>
                </tr>
                <tr>
                    <td align="center">
                    Lapse Days &nbsp;</td>
                    <td align="center">
                        <p>
                            &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
                        <input name="lapsedays" type="text" id="lapsedays" onkeyup="nospaces(this)"/></p>
                    <input name="Submit" type="submit" value="Submit" /></td>
                </tr>

            </table>
        </form>
        <p>
        &nbsp;</p>
    </body>
</html>
4

1 回答 1

0

您不能用于x==whitespace检查x正则表达式。使用以下内容:

whitespace.test(x);

这将返回真或假。

此外,您不应new RegExp为此使用,而应使用文字运算符:

var whitespace = /^\w*$/;
于 2012-12-08T12:24:43.090 回答