用JS實現簡單的登入驗證功能,js登入驗證

來源:互聯網
上載者:User

用JS實現簡單的登入驗證功能,js登入驗證

實現過程

代碼

<!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實現簡單的登入驗證功能,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.