天天看點

表單空白字元校驗

表單空白字元校驗! 如果使用者輸入資料 中間出現空白字元 如何校驗 該資料是非法的?

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: // 
  Time: 下午 :
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script type="application/javascript" src="${pageContext.request.contextPath}/JS/jquery-1.8.3.min.js"></script>
    <script>
        $(function () {
            //定義一個正規表達式
            //定義一個标簽
            var reg = /\s+/;
            var flag = false;
            $(":text,:password").blur(function () {
                //判斷裡面的值是否為空字元串
                if($(this).val()==""||reg.test($(this).val())){
                    flag = false;
                }else {
                    flag = true;
                }
            });
            //擷取點選按鈕
            $(":button").click(function () {
                if(flag){
                    $("#test").submit();
                }
            });
        })
    </script>
</head>
<body>
<form action="https://www.baidu.com" method="post" id="test">
    <form action="https://www.baidu.com" method="post">
        名字:<input type="text" name="名字">
        密碼:<input type="password" name="密碼">
        <input type="button" value="點我送出">
    </form>
</form>
</body>
</html>
           

繼續閱讀