您好,登錄后才能下訂單哦!
這篇文章主要介紹“JavaScript如何實(shí)現(xiàn)用戶名和密碼表單校驗(yàn)功能”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“JavaScript如何實(shí)現(xiàn)用戶名和密碼表單校驗(yàn)功能”文章能幫助大家解決問(wèn)題。
代碼如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>注冊(cè)頁(yè)面</title> <style> *{ margin: 0px; padding: 0px; box-sizing: border-box; } body{ background: url("img/register_bg.png") no-repeat center; padding-top: 25px; } .rg_layout{ width: 900px; height: 500px; border: 8px solid #EEEEEE; background-color: white; /*讓div水平居中*/ margin: auto; } .rg_left{ /*border: 1px solid red;*/ float: left; margin: 15px; } .rg_left > p:first-child{ color:#FFD026; font-size: 20px; } .rg_left > p:last-child{ color:#A6A6A6; font-size: 20px; } .rg_center{ float: left; /* border: 1px solid red;*/ } .rg_right{ /*border: 1px solid red;*/ float: right; margin: 15px; } .rg_right > p:first-child{ font-size: 15px; } .rg_right p a { color:pink; } .td_left{ width: 100px; text-align: right; height: 45px; } .td_right{ padding-left: 50px ; } #username,#password,#email,#name,#tel,#birthday,#checkcode{ width: 251px; height: 32px; border: 1px solid #A6A6A6 ; /*設(shè)置邊框圓角*/ border-radius: 5px; padding-left: 10px; } #checkcode{ width: 110px; } #img_check{ height: 32px; vertical-align: middle; } #btn_sub{ width: 150px; height: 40px; background-color: #FFD026; border: 1px solid #FFD026 ; } .error { color: red; } #td_sub { padding-left: 150px; } </style> <script> //實(shí)現(xiàn)賬戶密碼表單校驗(yàn) window.onload = function () { //給表單綁定onsubmit事件 //也就是點(diǎn)擊注冊(cè)按鈕就會(huì)驗(yàn)證用戶名和密碼是否正確 document.getElementById("form").onsubmit = function () { //調(diào)用用戶校驗(yàn)方法 checkUsername(); //調(diào)用密碼校驗(yàn)方法 checkPassword(); return checkUsername() && checkPassword(); } //給用戶名和密碼分別綁定離焦事件 //當(dāng)離焦時(shí)就會(huì)校驗(yàn)用戶名和密碼是否正確 //注意這里的方法沒(méi)有括號(hào) document.getElementById("username").onblur = checkUsername; document.getElementById("password").onblur = checkPassword; } //校驗(yàn)用戶名 function checkUsername() { //1、獲取用戶名的值 var username = document.getElementById("username").value; //2、定義正則表達(dá)式 var reg_username = /^w{6,12}$/; //3、判斷值是否符合正則的規(guī)則 var flag = reg_username.test(username); //4、提示信息 var s_username = document.getElementById("s_username"); if (flag) { //提示綠色對(duì)勾 s_username.innerHTML = "<img width="35" height="25" src="img/gou.png" />"; } else { //提示紅色用戶名有誤 s_username.innerHTML = "用戶名格式有誤"; } return flag; } //校驗(yàn)密碼 function checkPassword() { //1、獲取用戶名的值 var password = document.getElementById("password").value; //2、定義正則表達(dá)式 var reg_password = /^w{6,12}$/; //3、判斷值是否符合正則的規(guī)則 var flag = reg_password.test(password); //4、提示信息 var s_password = document.getElementById("s_password"); if (flag) { //提示綠色對(duì)勾 s_password.innerHTML = "<img width="35" height="25" src="img/gou.png" />"; } else { //提示紅色用戶名有誤 s_password.innerHTML = "密碼格式有誤"; } return flag; } </script> </head> <body> <div> <div> <p>新用戶注冊(cè)</p> <p>USER REGISTER</p> </div> <div> <div> <!--定義表單 form--> <form action="#" id="form" method="get"> <table> <tr> <td><label for="username">用戶名</label></td> <td> <input type="text" name="username" id="username" placeholder="請(qǐng)輸入用戶名"> <span id="s_username"></span> </td> </tr> <tr> <td><label for="password">密碼</label></td> <td> <input type="password" name="password" id="password" placeholder="請(qǐng)輸入密碼"> <span id="s_password"></span> </td> </tr> <tr> <td><label for="email">Email</label></td> <td><input type="email" name="email" id="email" placeholder="請(qǐng)輸入郵箱"></td> </tr> <tr> <td><label for="name">姓名</label></td> <td><input type="text" name="name" id="name" placeholder="請(qǐng)輸入姓名"></td> </tr> <tr> <td><label for="tel">手機(jī)號(hào)</label></td> <td><input type="text" name="tel" id="tel" placeholder="請(qǐng)輸入手機(jī)號(hào)"></td> </tr> <tr> <td><label>性別</label></td> <td> <input type="radio" name="gender" value="male"> 男 <input type="radio" name="gender" value="female"> 女 </td> </tr> <tr> <td><label for="birthday">出生日期</label></td> <td><input type="date" name="birthday" id="birthday" placeholder="請(qǐng)輸入出生日期"></td> </tr> <tr> <td><label for="checkcode" >驗(yàn)證碼</label></td> <td><input type="text" name="checkcode" id="checkcode" placeholder="請(qǐng)輸入驗(yàn)證碼"> <img id="img_check" src="img/verify_code.jpg"> </td> </tr> <tr> <td colspan="2" id="td_sub"><input type="submit" id="btn_sub" value="注冊(cè)"></td> </tr> </table> </form> </div> </div> <div> <p>已有賬號(hào)?<a href="#" >立即登錄</a></p> </div> </div> </body> </html>
關(guān)于“JavaScript如何實(shí)現(xiàn)用戶名和密碼表單校驗(yàn)功能”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。