JavaScript 基础,登录验证

1.<script></script>的三种用法:

  1. 放在<body>中
  2. 放在<head>中
  3. 放在外部JS文件中
  4. <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script>
            function displayDate() {
                document.getElementById("demo").innerHTML=Date();
            }
        </script>
        <script src="xl.js"></script>
    </head>
    <body>
    <p id="demo">11</p>
    <script>
        document.write(Date());
        document.getElementById("demo").innerHTML=Date();
    </script>
    </body>
    </html>
    放在外部JS文件中
    function myFuntion() {
        document.getElementById("demo").innerHTML="the first JavaScript";
    }

2.三种输出数据的方式:

  1. 使用 document.write() 方法将内容写到 HTML 文档中。
  2. 使用 window.alert() 弹出警告框。使用 innerHTML 写入到 HTML 元素。
    1. 使用 "id" 属性来标识 HTML 元素。
    2. 使用 document.getElementById(id) 方法访问 HTML 元素。
    3. 用innerHTML 来获取或插入元素内容。
      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
          <title>Title</title>
      </head>
      <body>
      <p id="demo">11</p>
      <script>
          document.write(Date());
          document.getElementById("demo").innerHTML=new Date();
      </script>
      <button type="button" onclick=window.alert("无法查找")>查询</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>江南皮革厂</title>
    <link rel="stylesheet" type="text/css" href="./static/shabi/denglukuang.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>20) && (oUpass.value.length<6 || oUpass.value.length>20)){
                oError.innerHTML = "用户名和密码都要6-20位"
            }
        }
    </script>
</head>
<body>
<hr>
<div class="bigdiv">
    <div><h3>登录</h3></div>
    <div>
            用户:<input id="uname" type="text"  placeholder="用户名">
    </div>
    <div>
            密码:<input id="upass" type="password"  placeholder="密码"><br>
    </div>
    <div id="error_box"><br></div>
    <div>
        <button class="button" onclick="fnLogin()">登录</button>
    </div>
    <div><h6>版权归江南皮革厂所有</h6></div>
    <p>如有疑问可联系小梁:13711269544</p>
</div>

</body>
</html>
css文件:
body{
    background-color: #6bffb3;
}
h3{
    font-size: 22px;
    padding-left: 40px;
    background-color: #65ffb7;
    margin-right: 20px;
    text-align: center;
}
.bigdiv{
    text-align: center;
    vertical-align: middle;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -150px 0 0 -150px;
    border: 1px solid #ccc;
     300px;
}
.button{
    margin-bottom: 10px;
    color: #a4ff51;
}

原文地址:https://www.cnblogs.com/951111ldj/p/7737736.html