Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript空白验证不起作用_Javascript_Jsp - Fatal编程技术网

JavaScript空白验证不起作用

JavaScript空白验证不起作用,javascript,jsp,Javascript,Jsp,我已经编写了JSP代码,我需要检查空白验证,但它不起作用。有人能告诉我原因吗。单击“提交”按钮时,必须验证是否有空格和字符 JSP代码 <%-- Document : LapsePeriodicFeedBack Created on : Dec 7, 2012, 2:50:28 PM Author : Admin --%> <%@page contentType="text/html" pageEncoding="UTF-8"%>

我已经编写了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>

失效周期表
函数validateForm(){
var selectindex=document.forms[“失效”][“周期菜单”]。selectedIndex;
var whitespace=newregexp(“^\w*$”);
var x=文件格式[“失效”][“失效天数”]。值;
var textlength=document.forms[“失效”][“失效天数”].length;
如果(x==“”){
警报(“天数不能为空”);
返回false;
}
if(x==空白){
警告(“不允许使用空白”);
返回false;
}
如果(selectindex==0){
警报(“请从菜单中选择天数”);
}
}
如果(x==“/\\s”){
警报(“对不起,您不允许输入任何空格”);
x、 value=x.value.替换(/\s/g',);
}
定期反馈表
定期反馈日
挑选
失效天数


您不能使用
x==空白
对正则表达式检查
x
。使用以下命令:

whitespace.test(x);
这将返回true或false

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

var whitespace = /^\w*$/;

定义不工作。我的车现在坏了。你能帮我把它也修好吗?验证不起作用,相反,允许文本框中的空白
/^\w*$/
也在开始和结束时被磨掉。我猜他想匹配任何空格字符,所以
/\w+/
如何在文本框中的字符之前检测空格,空格已经被删除了solved@Matt:经过深思熟虑,这是不正确的
\w
不是空白,而是非空白@user1858826:什么?如果(!whitespace.test(x)){alert(“不允许空白”);return false;}是我写的Callum Macrae,你能告诉我如何避免文本框中txt前的空白吗