JavaScript 基础,登录前端验证

Posted 叶远超

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 基础,登录前端验证相关的知识,希望对你有一定的参考价值。

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

a,放在<body>中

b.放在<head>中

c.放在外部JS文件中

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

a.使用 document.write() 方法将内容写到 html 文档中。

b.使用 window.alert() 弹出警告框。

c.使用 innerHTML 写入到 HTML 元素。

         1.使用 "id" 属性来标识 HTML 元素。

         2.使用 document.getElementById(id) 方法访问 HTML 元素。

         3.用innerHTML 来获取或插入元素内容。

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>时间</title>
    <script type="text/javascript" src="../static/js2.js"></script>
</head>
<body>
    <p>Hello</p>
    <script>
        document.write(Date())
    </script>
<button type="button" onclick=window.alert("用户名不能以数字开头")>Login</button>
</body>
</html>

3.登录页面准备:

a.增加错误提示框。

b.写好HTML+CSS文件。

c.设置每个输入元素的id

4.定义JavaScript 函数。

a.验证用户名6-20位

b.验证密码6-20位

c.onclick调用这个函数。

<!DOCTYPE html>
<html lang="en">
<head>

    <body bgcolor="#f5deb3">
    <meta charset="UTF-8">
    <title>游戏登陆</title>
    <link href="../static/css/text.css" rel="stylesheet" type="text/css">
    <script>
        function mylogin() {
            var oUname=document.getElementById("uname");
            var oError=document.getElementById("error_box");
            if(oUname.value.length<6||oUname.value.length>20){
            oError.innerHTML="用户名必须大于5位或少于20位!"
        }

        }
    </script>
</head>
<body >
<div class="y45">
           <h1>VIP玩家登录</h1>
 <div class="input_y45">
     <input id="uname" type="text" placeholder="请输入用户名">
 </div>
        <div class="input_y45">
            <input id="upass" type="password" placeholder="请输入密码">
        </div>
        <div id="error_y45"><br></div>
   <div class="input_y45">
       <button onclick="mylogin()">登录</button>
   </div>



    </div>
</body></body>
div{
    margin: 0 auto;
    text-align: center;
    background-color: #f0c5df;

}

.input_y45{
    width: 300px;
    height: 30px;
    border-bottom-color:coral;
    border-bottom-style: groove;
    border-bottom-width: 2px;
    line-height: 30px;
    font-weight: bold;
    background-color:seashell;
}
.input_y45{
    font-size: 8px;
    font-weight: bold;
    border-color: #85d6ff;
}
.error_y45{
     width: 300px;
    height: 30px;
    border-bottom-color: #85d6ff;
    border-bottom-style: groove;
    border-bottom-width: 2px;
    line-height: 30px;
    font-weight: bold;
    background-color: #ffbaa2;
}

以上是关于JavaScript 基础,登录前端验证的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 基础,登录前端验证

JavaScript 基础,登录前端验证

JavaScript 基础,登录前端验证

JavaScript 基础,登录前端验证

JavaScript 基础,登录前端验证

JavaScript 基础,登录前端验证