js登录验证

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 
        action:提交数据到服务器,跳转页面
     -->
    <form action="success.html" method="get">
        用户名:<input id="username" type="text" name="username" placeholder="用户名长度不能超过5位" >
        <br>
        密码:<input id="password" type="password" name="password" placeholder="密码长度不能超过5位" >
        <br>
        <button type="button" onclick="login()">登录</button>
    </form>
    <script>
      
        function login(){
            //username是input输入框对象的值 value属性来获取输入框的值
            var username=document.getElementById("username").value;
            var password=document.getElementById("password").value;
            //用户名不能为空并且长度不能超过5位
            // 密码不能超过6位
            //字符串的length属性表示字符串的长度
            if(username.length>5 || username ==""){
                alert("当前用户名输入不合法");
                //结束整个方法
                return;
            }
            if(password.length>6 || password ==""){
                alert("当前密码输入不合法");
                return;
            }
            //匹配用户名和密码
            if(username == "dhl" && password=="12345"){
                //验证成功,跳转成功页面
                //事件:用户与浏览器之间的行为交互
                //点击、鼠标移入、鼠标移出
                //通过JS强制跳转页面
                alert("登录成功");
                window.location.href="success.html";
            }
        }
        // 当用户名输入框值改变时,就去判断值的合法性
        var username=document.getElementById("username");
        username.onchange=function(){
            var value=username.value;
            if(value.length>5 || value ==""){
                alert("当前用户名输入不合法");
                //结束整个方法
                return;
            }
        }
        //失去焦点事件
        username.onblur=function(){
            alert("摸我干嘛");
        }
        username.onfocus=function(){
            alert("你还摸我");
        }
    </script>
</body>

</html>

 

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值