表單驗證
一、驗證的規則
驗證用戶名:必須由字母,數字下劃線組成,并且長度為 5 到 12 位 驗證密碼:必須由字母,數字下劃線組成,并且長度為 5 到 12 位 驗證確認密碼:和密碼相同 郵箱驗證:[email protected] 驗證碼:現在只需要驗證用戶已輸入,二、具體操作
1、新建一個模塊

2、把書城的靜態資源拷貝到 05_book_static 工程下:

3、具體驗證如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>尚硅谷會員注冊頁面</title> <link type="text/css" rel="stylesheet" href="../../static/css/style.css" > <script type="text/javascript" src="../../static/script/jquery-1.7.2.js"></script> <script type="text/javascript"> //頁面加載完成之后 $(function () { //給注冊系結單擊事件 $("#sub_btn").click(function () { // 驗證用戶名:必須由字母,數字下劃線組成,并且長度為 5 到 12 位 //1.獲取用戶名輸入框中的內容 var usernameText = $("#username").val(); //2.提供校驗規則:創建正則運算式物件 var usernamePatt = /^\w{5,12}$/; //3.使用test方法進行校驗 if(!usernamePatt.test(usernameText)){ //4.提示用戶結果 $("span.errorMsg").text("用戶名不合法"); //阻止表單提交 return false; } // 驗證密碼:必須由字母,數字下劃線組成,并且長度為 5 到 12 位 //1.獲取密碼輸入框中的內容 var passwordText = $("#password").val(); //2.提供校驗規則:創建正則運算式物件 var passwordPatt = /^\w{5,12}$/; //3.使用test方法進行校驗 if(!passwordPatt.test(passwordText)){ //4.提示用戶結果 $("span.errorMsg").text("密碼不合法"); //阻止表單提交 return false; } //驗證確認密碼是否和密碼相同 //1.獲取確認密碼輸入框的內容 var repwdText = $("#repwd").val(); //2.和密碼相比較 if (repwdText != passwordText){ //3.提示用戶 $("span.errorMsg").text("確認密碼和密碼不一致"); //阻止表單提交 return false; } //驗證郵箱:[email protected] //1.獲取郵箱輸入框中的內容 var emailText = $("#email").val(); //2.創建正則運算式物件 var emailPatt = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/; //3.使用test方法進行校驗 if (!emailPatt.test(emailText)){ //4.提示用戶 $("span.errorMsg").text("郵箱格式不合法"); //阻止表單提交 return false; } //驗證碼:只需要驗證用戶已輸入 //1.獲取驗證碼輸入框的內容 var codeText = $("#code").val(); //2.去掉驗證碼的前后空格 codeText = $.trim(codeText); //3.如果驗證碼為null或空串,提示用戶 if (codeText == null || codeText == ""){ //4.提示用戶 $("span.errorMsg").text("驗證碼不能為空"); //阻止表單提交 return false; } //注意點:在表單提交的程序中,如果網路出現了例外,即使我們將驗證碼修改正確,錯誤提示資訊還在, //因此,需要重新將span標簽里的內容設定為空, $("span.errorMsg").text(""); }); }); </script> <style type="text/css"> .login_form{ height:420px; margin-top: 25px; } </style> </head> <body> <div id="login_header"> <img class="logo_img" alt="" src="../../static/img/logo.gif" > </div> <div class="login_banner"> <div id="l_content"> <span class="login_word">歡迎注冊</span> </div> <div id="content"> <div class="login_form"> <div class="login_box"> <div class="tit"> <h1>注冊尚硅谷會員</h1> <span class="errorMsg"></span> </div> <div class="form"> <form action="regist_success.html"> <label>用戶名稱:</label> <input class="itxt" type="text" placeholder="請輸入用戶名" autocomplete="off" tabindex="1" name="username" id="username" /> <br /> <br /> <label>用戶密碼:</label> <input class="itxt" type="password" placeholder="請輸入密碼" autocomplete="off" tabindex="1" name="password" id="password" /> <br /> <br /> <label>確認密碼:</label> <input class="itxt" type="password" placeholder="確認密碼" autocomplete="off" tabindex="1" name="repwd" id="repwd" /> <br /> <br /> <label>電子郵件:</label> <input class="itxt" type="text" placeholder="請輸入郵箱地址" autocomplete="off" tabindex="1" name="email" id="email" /> <br /> <br /> <label>驗證碼:</label> <input class="itxt" type="text" style="width: 150px;" id="code"/> <img alt="" src="../../static/img/code.bmp" style="float: right; margin-right: 40px"> <br /> <br /> <input type="submit" value="注冊" id="sub_btn" /> </form> </div> </div> </div> </div> </div> <div id="bottom"> <span> 尚硅谷書城.Copyright ©2015 </span> </div> </body> </html>
注:
對正則運算式比較陌生的話,可以去jQuery提供的API檔案直接復制粘貼具體的驗證規則:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/209255.html
標籤:其他
上一篇:git和pycharm連接
