實現過程示意圖

代碼
<!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> //校驗賬號的格式 function check_code() { console.log(1); //獲取賬號 var code = document.getElementById("code").value; //校驗其格式(/w字母或數字或下劃線) var span = document.getElementById("code_msg"); var reg = /^/w{6,10}$/; if(reg.test(code)) { //通過,增加ok樣式 span.className = "ok"; } else { //不通過,增加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> 賬號: <input type="text" id="code" onblur="check_code()"/> <span id="code_msg">6-10位字母、數字、下劃線</span> </p> <p> 密碼: <input type="text" id="pwd" onblur="check_pwd()" /> <span id="pwd_msg">8-20位字母、數字、下劃線</span> </p> <p><input type="submit" value="登錄"/></p> </form> </body> </html>下面在看一段簡單的代碼關于javascript實現簡單的用戶登錄驗證
代碼如下所示:
<!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("沒有輸入用戶名!"); return false; } else if(document.getElementById("password").value=="") { alert("沒有輸入密碼!"); 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>總結
以上所述是小編給大家介紹的用JS實現簡單的登錄驗證功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
新聞熱點
疑難解答