JavaScript 基础,登录验证

    1. <script></script>的三种用法:
      1. 放在<body>中
      2. 放在<head>中
      3. 放在外部JS文件中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript" src="../static/ddd/dd.js"></script>
    <script type="text/javascript">alert('这是head里面的javascript代码')</script>
</head>
<body>

<p id="demo">fff</p>
<script>
    document.getElementById('demo').innerHTML = Date()
</script>

<button type="button" onclick=window.alert("number")>press</button>
</body>
</html>

js代码

alert('这是js文件里的javascript代码')

 

三种输出数据的方式:

    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>fff</title>
</head>
<body>

<p id="demo">fff</p>
<script>
    document.getElementById('demo').innerHTML=Date()
</script>

<button type="button" onclick=window.alert("number")>press</button>
</body>
</html>

 



登录页面准备:

  1. 增加错误提示框。
  2. 写好HTML+CSS文件。
  3. 设置每个输入元素的id

定义JavaScript 函数。

    1. 验证用户名6-20位
    2. 验证密码6-20位。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link rel="stylesheet" type="text/css" href="../static/css/1027.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><h1>登录</h1></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><h2>guo@</h2></div>

</div>

</body>
</html>

css

body{
    background-color: beige;
}
h1{
    font-size: 22px;

    background-color: cornflowerblue;

    text-align: center;

}
.bigdiv{
    text-align: center;
    vertical-align: middle;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -150px 0 0 -150px;
    border: 1px solid #000000;
    width: 300px;
}
.button{
    margin-bottom: 10px;
    color: black;
}


onclick调用这个函数


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>fff</title>
</head>
<body>

<button type="button" onclick=window.alert("number")>press</button>

</body>
</html>

 







posted @ 2017-10-27 17:23  029郭媚婷  阅读(110)  评论(0编辑  收藏  举报