JS實戰 · 表單驗證

來源:互聯網
上載者:User

標籤:val   info   執行   校正   isp   ext   meta   type   win   

思路:        1、定義頁面            通過表格格式化表單;            表格行都有自己的背景顏色;            儲存格中的資料(文本等)用div進行封裝,好操作;        2、定義樣式            表格的樣式;            div的樣式;        3、動態效果            頁面載入時,將所有的輸入框定義預設框線顏色和擷取焦點時的框線顏色;            進行內容校正,不正確時顯示警告資訊。 代碼如下:<html><head>    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">    <title>表單驗證</title>    <!-- 思路:        1、定義頁面            通過表格格式化表單;            表格行都有自己的背景顏色;            儲存格中的資料(文本等)用div進行封裝,好操作;        2、定義樣式            表格的樣式;            div的樣式;        3、動態效果            頁面載入時,將所有的輸入框定義預設框線顏色和擷取焦點時的框線顏色;            進行內容校正,不正確時顯示警告資訊。     -->    <style type="text/css">        table{            border: #A50DFA 1px solid;            width: 500px;            border-collapse: collapse;        }        table th, table td{            border: #A50DFA 1px solid;            padding:10px;        }        table th{                    }        table td{                    }        .errorinfo{            color: #EA1508;            font-family: "華文行楷";            font-weight: bold;            display: none;        }        .focus{            border: #03F2FC 2px solid;        }        .outs{            border: #81710D 1px solid;        }        .error{            border: #FA0324 2px solid;        }    </style>    <script type="text/javascript">        function checkinput(input){            input.className = "outs";            input.onfocus = function(){                this.className = "focus";            }        }        /*視窗載入時就執行*/        window.onload = function(){            with(document.forms[0]){                checkinput(user);                checkinput(psw);                checkinput(repsw);                checkinput(mail);            }        }        /*驗證輸入的資料*/        /*方法一:每個輸入框一個function,代碼重複性高,捨棄不用。*/        function checkDate(userNode){            var value = userNode.value;            var regex = /^\w{3,5}$/;            var divNode = document.getElementById("userdiv");            if(regex.test(value)){                userNode.className = "outs";                divNode.style.display = "none";            }else{                userNode.className = "error";                divNode.style.display = "block";            }        }        /*方法二*/        function check(inputNode, regex, divId){            var divNode = document.getElementById(divId);            if(regex.test(inputNode.value)){                inputNode.className = "outs";                divNode.style.display = "none";            }else{                inputNode.className = "error";                divNode.style.display = "block";            }        }         /*驗證使用者名稱*/         function checkUser(userNode){            var regex = /^\w{3,5}$/;            check(userNode, regex, "userdiv");        }          /*驗證密碼格式*/         function checkPsw(pswNode){            var regex = /^[a-z0-9]{4,8}$/i;            check(pswNode, regex, "pswdiv");        }        /*驗證兩次密碼是否一致*/         function checkRepsw(repswNode){            var psw = document.getElementById("psw").value;            var repsw = repswNode.value;            var divNode = document.getElementById("repswdiv");            if(psw == repsw){                repswNode.className = "outs";                divNode.style.display = "none";            }else{                repswNode.className = "error";                divNode.style.display = "block";            }        }      /*驗證郵箱*/        function checkMail(mailNode){            var regex = /^\[email protected]\w+(\.\w+)+$/;            check(mailNode, regex, "maildiv");        }    </script></head><body>    <form>        <table>            <tr>                <th>使用者註冊</th>            </tr>            <tr>                <td>                    <div>使用者名稱</div>                    <div>                        <input type="text" name="user" onblur="checkUser(this)"/>                    </div>                    <div class="errorinfo" id="userdiv">使用者名稱填寫錯誤。</div>                    <div>使用者名稱必須是3-5位,由字母(a-z)、數字(0-9)、底線(_)組成</div>                </td>            </tr>            <tr>                <td>                    <div>密碼</div>                    <div>                        <input type="password" name="psw" id="psw" onblur="checkPsw(this)"/>                    </div>                    <div class="errorinfo" id="pswdiv">密碼格式錯誤。</div><br/>                    <div>確認密碼</div>                    <div>                        <input type="password" name="repsw"  onblur="checkRepsw(this)"/>                    </div>                    <div class="errorinfo" id="repswdiv">兩次輸入密碼不一致。</div>                    <div>密碼必須是4-8位,由字母(a-z)、數字(0-9)組成</div>                </td>            </tr>            <tr>                <td>                    <div>郵箱</div>                    <div>                        <input type="text" name="mail" onblur="checkMail(this)"/>                    </div>                    <div class="errorinfo" id="maildiv">郵箱格式錯誤,請規範填寫。</div>                </td>            </tr>            <tr>                <th><input type="button" value="提交資料" onclick="submitDate()"></th>            </tr>        </table>    </form></body></html>     

JS實戰 · 表單驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.