简体   繁体   English

JavaScript空白验证不起作用

[英]JavaScript White Space Validation not working

I have written JSP code and I need to check white space validation, But it is not working can someone tell me the reason. 我已经编写了JSP代码,并且需要检查空白验证,但是无法正常工作有人可以告诉我原因。 While clicking submit button it must validate for white space and characters. 单击提交按钮时,它必须验证空格和字符。

JSP Code 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>

You can't use x==whitespace to check x against a regular expression. 您不能使用x==whitespace来针对正则表达式检查x Use the following: 使用以下内容:

whitespace.test(x);

That'll return true or false. 这将返回true或false。

In addition, you should not use new RegExp for this, you should instead use the literal operators: 此外,您不应为此使用new RegExp ,而应使用文字运算符:

var whitespace = /^\w*$/;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM