JavaScript 基礎,登陸驗證

    1. <script></script>的三種用法:
      1. 放在<body>中
      2. 放在<head>中
      3. 放在外部JS文件中
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>text1</title>
            <script>
                function displayDate() {
                    document.getElementById("text1").innerHTML=Date();
                }
            </script>
            <script src="1.js"></script>
        </head>
        <body>
              <p>Hello</p>
              <script>
                  document.write(Date())
                  document.getElementById("demo").innerHTML=Date();
              </script>
          <button type="button" onclick=window.alert("用戶名不能是數字開頭的")>button</button>
        </body>
        </html>

         

         

         

    2. 三種輸出數據的方式:
      1. 使用 document.write() 方法將內容寫到 HTML 文檔中。
      2. 使用 window.alert() 彈出警告框。
      3. 使用 innerHTML 寫入到 HTML 元素。
        1. 使用 "id" 屬性來標識 HTML 元素。
        2. 使用 document.getElementById(id) 方法訪問 HTML 元素。
        3. 用innerHTML 來獲取或插入元素內容。
          <!DOCTYPE html>
          <html lang="en">
          <head>
              <meta charset="UTF-8">
              <title>text2</title>
          </head>
          <body>
                <p id="text2">111</p>
                <script>
                    document.getElementById("demo").innerHTML = Date();
                </script>
            <button type="button" onclick=window.alert("用戶名不能是數字開頭的")>button</button>
          </body>
          </html>

           

    3. 登陸頁面準備:
      1. 增長錯誤提示框。
      2. 寫好HTML+CSS文件。
      3. 設置每一個輸入元素的id
    4. 定義JavaScript 函數。
      1. 驗證用戶名6-20位
      2. 驗證密碼6-20位
    5. onclick調用這個函數。
      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>text3</title>
          <link rel="stylesheet"type="text/css" href="../static/css/123.css">
          <script>
              function fnLogin() {
                  var oUname=document.getElementById("uname");
                  var oUpass=document.getElementById("upass");
                  var oError=document.getElementById("error_box");
                  if (oUname.value.length < 6 || oUname.value.length > 20) {
                      oError.innerHTML = "請輸入6-20位的用戶名"
                  }
                  if (oUpass.value.length < 6 || oUpass.value.length > 20) {
                      oError.innerHTML = "請輸入6-20位的密碼"
                  }
      
                  if ((oUname.value.length < 6 || oUname.value.length > 10) && (oUpass.value.length < 6 || oUpass.value.length > 20)) {
                      oError.innerHTML = "請輸入6-20位的用戶名和密碼"
                  }
              }
          </script>
      
      </head>
      <body>
      <div class="box">
          <h2>登陸</h2>
          <div class="input_box">
              <input id="uname" type="text"placeholder="請輸入用戶名">
          </div>
          <div class="input_box">
              <input  id="upass" type="text"placeholder="請輸入密碼">
          </div>
          <div id="error_box"><br></div>
          <div class="input_box">
              <button class="button" onclick="fnLogin()">登陸</button>
          </div>
      
      
      </div>
      </body>
      </html>
      body{
      background-color: yellow;
      }

相關文章
相關標籤/搜索