关于密码强度实现

来源:2-25 项目作业

江舟

2021-05-22 23:15:22

老师,注册的密码强度js实现是否有最佳实践,能否对其进行下讲解?

我的实现方式是通过数组存储三个正则表达式,分别去匹配数字、小写字母、大写字母,如果其中匹配结果为true的个数即密码强度的级数。但是这种实现方式需要依赖另一个存储颜色的数组,这两个数组关联性太高,可修改性低。

完整代码如下:

<!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>register</title>

    <style>

        html{

            height100%;

        }

        body{

            margin0;

            padding0;

            height100%;

            backgroundlinear-gradient(#141e30#243b55);

        }

        .register-outside{

            width800px;

            positionabsolute;

            left50%;

            top:50%;

            transformtranslate(-50%,-50%);

        }

        .register{

            width400px;

            margin0 auto;

            box-sizingborder-box;

            padding0 20px;

            background-colorrgba(000.5);

            box-shadow0 15px 20px rgba(000.6);

            colorwhite;

        }

        .register h2{

            text-aligncenter;

            padding20px 0;

            margin0;

        }

        .register .user-box{

            padding-left20px;

            margin-bottom20px;

        }

        .register .user-box .register-label{

            floatleft;

            padding-top5px;

            width80px;

            font-size14px;

        }

        .register .user-box .require::before{

            content"*";

            positionabsolute;

            margin-left-1em;

            margin-top0.5em;

            colorred;

            font-size14px;

        }

        .register .user-box .register-cell{

            displaytable-cell;

        }

        .register .user-box input{

            background-colortransparent;

            font-size18px;

            outlinenone;

            box-sizingborder-box;

            padding5px 20px 5px 0px;

            colorwhite;

            bordernone;

            border-bottom1px solid white;

        }

        .register #submit{

            displayblock;

            box-sizingborder-box;

            padding10px 20px;

            width50%;

            margin20px auto;

            font-size16px;

            color#03e9f4;

            background-colortransparent;

            bordernone;

            letter-spacing1em;

            cursorpointer;

            border-radius5px;

            transitionall .5s 0s ease;

        }

        .register #submit:hover{

            colorwhite;

            background-color#03e9f4;

            box-shadow0 0px 5px #03e9f4,

                        0 0px 25px #03e9f4,

                        0 0px 50px #03e9f4,

                        0 0px 100px #03e9f4;

        }

        .register .register-remark{

            positionabsolute;

            margin-left1em;

            margin-top1em;

            height1em;

        }

        .register .register-remark i{

            displayinline-block;

            width3px;

            height1em;

            background-color#fff;

            margin-right0.5em;

        }


        .red{

            colorred;

        }

    </style>

</head>

<body>

    <section class="register-outside">

        <div class="register">

            <h2>注册</h2>

            <form action="">

                <div class="user-box">

                    <label for="u_name" class="register-label require">用户名</label>

                    <div class="register-cell">

                        <input type="text" name="u_name" id="u_name">

                        <span class="register-remark">

                        </span>

                    </div>

                </div>

                <div class="user-box">

                    <label for="pwd_f" class="register-label require">登录密码</label>

                    <div class="register-cell">

                        <input type="password" name="pwd_f" id="pwd_f" placeholder="6-20位字母或数字">

                        <span class="register-remark">

                            <i></i>

                            <i></i>

                            <i></i>

                            <span class="red"></span>

                        </span>

                    </div>

                </div>

                <div class="user-box">

                    <label for="pwd_s" class="register-label require">确认密码</label>

                    <div class="register-cell">

                        <input type="password" name="pwd_s" id="pwd_s">

                        <span class="register-remark">

                        </span>

                    </div>

                </div>

                <div class="user-box">

                    <label for="email" class="register-label">邮箱</label>

                    <div class="register-cell">

                        <input type="text" name="email" id="email">

                    </div>

                </div>

                <input type="submit" value="SUBMIT" id="submit">

            </form>

        </div>

    </section>



    <script>

        let u_name = document.getElementById("u_name");

        let pwd_f = document.getElementById("pwd_f");

        let pwd_s = document.getElementById("pwd_s");

        let canSubmit = {userName:false,firstPwd:false,secondPwd:false};

        u_name.onblur=function(e){

            if(/^[a-zA-Z]\w{5,29}$/.test(this.value)){

                this.nextElementSibling.innerHTML = "用户名输入正确";

                this.nextElementSibling.style.color = "green";

                canSubmit.userName = true;

            }else{

                this.nextElementSibling.innerHTML = "6-30位字母、数字或’_’,字母开头";

                this.nextElementSibling.style.color = "red";

                canSubmit.userName = false;

            }

        }

        let pwdStrength = [/\d/,/[a-z]/,/[A-Z]/];

        let bgc = ["red","yellow","green"];

        pwd_f.onblur = function (e) {

            this.nextElementSibling.lastElementChild.innerHTML = "";

            bgc.map((n,pos)=>pos).forEach(pos=>this.nextElementSibling.children[pos].style.backgroundColor="");

            if(/[a-zA-Z0-9]{6,20}/.test(this.value)){

                let index = pwdStrength.map(pattern=>pattern.test(this.value))

                                        .reduce((arr,cur)=>cur?arr+1:arr,0)-1;

                bgc.forEach((color,pos)=>{

                    if(pos<=index){

                        this.nextElementSibling.children[pos].style.backgroundColor = color;

                    }else{

                        this.nextElementSibling.children[pos].style.backgroundColor = "";

                    }

                })

             

            canSubmit.firstPwd = true;

            }else{

                this.nextElementSibling.lastElementChild.innerHTML = "6-20位字母或数字";

                canSubmit.firstPwd = false;

            }

        }

        pwd_s.onblur = function(e){

            this.nextElementSibling.style.color="red";

            canSubmit.secondPwd = false;

            if(this.value.length===0){

                this.nextElementSibling.innerHTML = "输入框不能为空";

            }else if(this.value === pwd_f.value){

                this.nextElementSibling.innerHTML = "两次输入一致";

                this.nextElementSibling.style.color = "green";

                canSubmit.secondPwd = true;

            }else{

                this.nextElementSibling.innerHTML = "两次密码输入不一致,请重新输入";

            }

        }

        document.getElementById("submit").onclick = function(e){

            if(Object.keys(canSubmit).every(key => canSubmit[key]))alert("信息填写正确");

            else alert("请填写正确信息");

            e.preventDefault();

        }

    </script>

</body>

</html>


写回答

1回答

好帮手慕星星

2021-05-23

同学你好,用数组实现比一个一个的判断代码少一些,不过可能理解起来复杂,是不错的,效果上没问题。

祝学习愉快!

0

0 学习 · 15276 问题

查看课程