簡體   English   中英

JavaScript空白驗證不起作用

[英]JavaScript White Space Validation not working

我已經編寫了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>

您不能使用x==whitespace來針對正則表達式檢查x 使用以下內容:

whitespace.test(x);

這將返回true或false。

此外,您不應為此使用new RegExp ,而應使用文字運算符:

var whitespace = /^\w*$/;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM