JavaScript登入

weixin_46585187發表於2020-10-28

需要賬號是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>

程式碼執行效果
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

相關文章