JavaScript 基础,登录前端验证

    1. <script></script>的三种用法:
      1. 放在<body>中
      2. 放在<head>中
      3. 放在外部JS文件中
    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>jsDemo</title>
          </head>
          <body>
               <p>HELLO</p>
               <script>
                   document.write(Date())
               </script>
               <button type="button" onclick=window.alert("用户名不能以数字开头")>登录</button>
          </body>
          </html>
          

            

          <!DOCTYPE html>
          <html lang="en">
          <head>
              <meta charset="UTF-8">
              <title>jsDemo</title>
          </head>
          <body>
               <p id="demo">HELLO</p>
               <script>
                   document.getElementById("demo").innerHTML=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>
          <script>
              function myLogin() {
                   var one=document.getElementById("un");
                   var error=document.getElementById("error_box");
                   if(one.value.length>6||one.value.length<20){
                          error.innerHTML="用户名或密码输入有误!"
                   }
                }
          </script>
      
      </head>
      <body class="center">
          <h1>城市供水云服务平台</h1><br>
            <div  id="container" style="width:400px " >
             <div id="header" style="background-color: blue;"><h2 align="center" style="margin-bottom:0;">用户登录</h2></div>
             <div id="content" style="background-color:#EEEEEE;height:200px;width:400px;float:left;text-align:center">
                <div class="input_box">
                    <br>用户名:<input id="un" type="text" name="uname" placeholder="请输入用户名"><br>
                </div>
                 <div class="input_box">
                     <br>密码:<input id="pwd" type="password" name="pass"><br>
                 </div>
                     <br><input type="radio">城市用户
                     <input type="radio">访客<br>
                    <div class="input_box">
                     <br><input type="submit" value="登录" onclick="myLogin()">
      
                <div id="error_box"><br></div>
                    </div>
             </div>
                <div id="footer" style="background-color:blue;clear:both;text-align:center;">版权 © suxihong</div>
      
      </div>
      
      </body>
      </html>
      <button type="button" onclick=window.alert("输入有误!")></button>
      

        

       

posted on 2017-10-27 17:35  072苏喜虹  阅读(111)  评论(0)    收藏  举报

导航