用JS實(shí)現(xiàn)簡(jiǎn)單的登錄驗(yàn)證功能
實(shí)現(xiàn)過(guò)程示意圖
代碼
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登錄</title> <style> .ok { color: green; border: 1px solid green; } .error { color: red; border: 1px solid red; } </style> <script> //校驗(yàn)賬號(hào)的格式 function check_code() { console.log(1); //獲取賬號(hào) var code = document.getElementById("code").value; //校驗(yàn)其格式(\w字母或數(shù)字或下劃線) var span = document.getElementById("code_msg"); var reg = /^\w{6,10}$/; if(reg.test(code)) { //通過(guò),增加ok樣式 span.className = "ok"; } else { //不通過(guò),增加error樣式 span.className = "error"; } } function check_pwd(){ console.log(2); var code2 =document.getElementById("pwd").value; var span2 = document.getElementById("pwd_msg"); var reg2 = /^\w{6,10}$/; if(reg2.test(code2)) { span2.className = "ok"; } else { span2.className = "error"; } } </script> </head> <body> <form action="http://www.tmooc.cn"> <p> 賬號(hào): <input type="text" id="code" onblur="check_code()"/> <span id="code_msg">6-10位字母、數(shù)字、下劃線</span> </p> <p> 密碼: <input type="text" id="pwd" onblur="check_pwd()" /> <span id="pwd_msg">8-20位字母、數(shù)字、下劃線</span> </p> <p><input type="submit" value="登錄"/></p> </form> </body> </html>
下面在看一段簡(jiǎn)單的代碼關(guān)于javascript實(shí)現(xiàn)簡(jiǎn)單的用戶登錄驗(yàn)證
代碼如下所示:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script type="text/javascript"> function check() { if(document.getElementById("username").value=="") { alert("沒(méi)有輸入用戶名!"); return false; } else if(document.getElementById("password").value=="") { alert("沒(méi)有輸入密碼!"); return false; } else { alert("提交成功!") return true; } } </script> <form name="form1"> <input type="text" id="username"> <input type="password" id="password" > <input type="submit" onclick="check()"> </form> </body> </html>
總結(jié)
以上所述是小編給大家介紹的用JS實(shí)現(xiàn)簡(jiǎn)單的登錄驗(yàn)證功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- HTML頁(yè)面登錄時(shí)的JS驗(yàn)證方法
- js實(shí)現(xiàn)登錄注冊(cè)框手機(jī)號(hào)和驗(yàn)證碼校驗(yàn)(前端部分)
- 原生js驗(yàn)證簡(jiǎn)潔注冊(cè)登錄頁(yè)面
- JSP實(shí)現(xiàn)登錄功能之添加驗(yàn)證碼
- javascript和jquery實(shí)現(xiàn)用戶登錄驗(yàn)證
- 詳解JS實(shí)現(xiàn)系統(tǒng)登錄頁(yè)的登錄和驗(yàn)證
- js登錄滑動(dòng)驗(yàn)證的實(shí)現(xiàn)(不滑動(dòng)無(wú)法登陸)
- JSP + Servlet實(shí)現(xiàn)生成登錄驗(yàn)證碼示例
- Ionic+AngularJS實(shí)現(xiàn)登錄和注冊(cè)帶驗(yàn)證功能
- js實(shí)現(xiàn)滑動(dòng)滑塊驗(yàn)證登錄
相關(guān)文章
非主流的textarea自增長(zhǎng)實(shí)現(xiàn)js代碼
今天稍微研究了下textarea隨輸入內(nèi)容自動(dòng)增長(zhǎng)的功能,通過(guò)google參考了一些實(shí)現(xiàn)方式2011-12-12JavaScript delete操作符應(yīng)用實(shí)例
delete 運(yùn)算符 從對(duì)象中刪除一個(gè)屬性,或從數(shù)組中刪除一個(gè)元素。2009-01-01無(wú)編譯/無(wú)服務(wù)器實(shí)現(xiàn)瀏覽器的CommonJS模塊化
這篇文章主要介紹了無(wú)編譯/無(wú)服務(wù)器實(shí)現(xiàn)瀏覽器的CommonJS模塊化,對(duì)模塊化感興趣的同學(xué),可以參考下2021-05-05js點(diǎn)擊出現(xiàn)懸浮窗效果不使用JQuery插件
JQuery有很多這樣的插件,但本文的這個(gè)是跟著自己的想法寫(xiě)的,也不知道他人是如何實(shí)現(xiàn)的,感興趣的朋友可以了解下2014-01-01