简体   繁体   中英

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. While clicking submit button it must validate for white space and characters.

JSP Code

<%-- 
    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. Use the following:

whitespace.test(x);

That'll return true or false.

In addition, you should not use new RegExp for this, you should instead use the literal operators:

var whitespace = /^\w*$/;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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