JavaScript登录

需要账号是3-15位并且不能数字开头,密码:6-18位并且不能全是数字或者全是字符
判断账号

 //检查账号为3-15位&&不能数字开头
    function checkUserName() {
        var userName = document.getElementById("userName").value;
        var userNameShowDom = document.getElementById("userNameShow");
        if (userName.length >= 3 && userName.length <= 15) {
            //符合长度要求
            //不是数字开头的
            var c = userName.charAt(0);
            if (isNaN(c)) {
                //非数字开头--符合要求
                userNameShowDom.innerText = "验证通过";
                userNameShowDom.style.color = "green";
                return true;
            } else {
                //数字开头
                userNameShowDom.innerText = "不能以数字开头";
                userNameShowDom.style.color = "red";
                return false;
            }
        } else {
            //不符合长度要求
            userNameShowDom.innerText = "长度不符合要求";
            userNameShowDom.style.color = "red";
            return false;
        }
    }

判断密码

 function checkPwd() {
        var pwd = document.getElementById("pwd").value;
        var pwdShowDom = document.getElementById("pwdShow");
        if (pwd.length >= 6 && pwd.length <= 18) {
            //数字和字符
            var pwdReg = /(\D+\d+)|(\d+\D+)/;//同时含有数字和字符
            if(pwdReg.test(pwd)){
                pwdShowDom.innerText = "验证通过";
                pwdShowDom.style.color = "green";
                return true;
            }else{
                pwdShowDom.innerText = "必须同时含有数字和字符";
                pwdShowDom.style.color = "red";
                return false;
            }
        }else{
            pwdShowDom.innerText = "长度不符合要求";
            pwdShowDom.style.color="red";
            return false;
        }
    }

全部代码

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
账号:<input type="text" id="userName" placeholder="3-15位&&不能数字开头" onblur="checkUserName()"/>
<span id="userNameShow"></span><br/>
密码:<input type="text" id="pwd" placeholder="6-18位&&包含字母和数字" onblur="checkPwd()"/>
<span id="pwdShow"></span><br/>
<input type="submit" value="注册" onclick="reg()"/>
<script>
    //检查账号为3-15位&&不能数字开头
    function checkUserName() {
        var userName = document.getElementById("userName").value;
        var userNameShowDom = document.getElementById("userNameShow");
        if (userName.length >= 3 && userName.length <= 15) {
            //符合长度要求
            //不是数字开头的
            var c = userName.charAt(0);
            if (isNaN(c)) {
                //非数字开头--符合要求
                userNameShowDom.innerText = "验证通过";
                userNameShowDom.style.color = "green";
                return true;
            } else {
                //数字开头
                userNameShowDom.innerText = "不能以数字开头";
                userNameShowDom.style.color = "red";
                return false;
            }
        } else {
            //不符合长度要求
            userNameShowDom.innerText = "长度不符合要求";
            userNameShowDom.style.color = "red";
            return false;
        }
    }
    function checkPwd() {
        var pwd = document.getElementById("pwd").value;
        var pwdShowDom = document.getElementById("pwdShow");
        if (pwd.length >= 6 && pwd.length <= 18) {
            //数字和字符
            var pwdReg = /(\D+\d+)|(\d+\D+)/;//同时含有数字和字符
            if(pwdReg.test(pwd)){
                pwdShowDom.innerText = "验证通过";
                pwdShowDom.style.color = "green";
                return true;
            }else{
                pwdShowDom.innerText = "必须同时含有数字和字符";
                pwdShowDom.style.color = "red";
                return false;
            }
        }else{
            pwdShowDom.innerText = "长度不符合要求";
            pwdShowDom.style.color="red";
            return false;
        }
    }
    function reg(){
        if(checkUserName()&&checkPwd()){
            alert("注册成功")
        }
    }
</script>
</body>
</html>

代码运行效果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页