老师这道题目怎么判断密码强度为等级二

老师这道题目怎么判断密码强度为等级二

怎么判断同时密码满足两种类型的结合,包括小写字母大写字母,小写字母数字,还有大写字母数字,并且还保持在6-20位,实在想不出来,老师帮我改改代码

<!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>
    <style>
        i{
            font-style: normal;
            color: gray;
            padding-right: 5px;
            position: relative;
            top: 3px;
        }
        .box{
            width: 600px;
            height: 200px;
            padding: 50px;
            border: 1px solid orange;
            border-radius: 10px;
            position: relative;
            margin: 50px auto;
        }
        .box .tl{
            font-size: 14px;
        }
        .box .nbsp{
            width: 7px;
            display: inline-block;
        }
        .box input[type="text"]{
            width: 180px;
            margin-left: 10px;
            font-size: 12px;
            height: 23px;
            border-radius: 3px;
            padding-left: 5px;
            border: 1px solid gray;
        }
        .box .user-warning{
            color: green;
            font-size: 14px;
            padding-left: 5px;
        }
        .box .password-warning-text{
            color: red;
            font-size: 14px;
            padding-left: 5px;
            display: none;
        }
        .box .password-warning-box{
            padding-left: 5px;
        }
        .box .password-warning-box span{
            width: 40px;
            height: 6px;
            background-color: #ccc;
            display: inline-block;
        }
        .box .confirm_warning{
            font-size: 14px;
            color: red;
            padding-left: 5px;
            display: none;
        }
        .regis_btn{
            width: 180px;
            height: 35px;
            background-color: green;
            outline: none;
            border: none;
            border-radius: 5px;
            color: #fff;
            font-size: 14px;
            position: absolute;
            bottom: 30px;
            left: 240px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="box">
        <p>
            <i>*</i><span class="tl">用<span class="nbsp"></span>户<span class="nbsp"></span>名 :</span> 
            <input type="text" placeholder="用户名设置成功后不可更改" id="userField">
            <span class="user-warning">6-30位字母、数字或"_",字母开头</span>
        </p>
        <p>
            <i>*</i><span class="tl">登录密码 :</span>
            <input type="text" placeholder="6-20位字母或数字" id="passwordField">
            <span class="password-warning-text">6-20位字母或数字</span>
            <span class="password-warning-box">
                <span></span>
                <span></span>
                <span></span>
            </span>
        </p>
        <p>
            <i>*</i><span class="tl">确认密码 :</span>
            <input type="text" placeholder="再次输入您的登录密码">
            <span class="confirm_warning">输入框不能为空</span>
        </p>
        <button class="regis_btn">注册</button>
    </div>
    <script>
        var userField=document.getElementById('userField');
        var userWarning=document.querySelector('.user-warning');
        //判断用户名是否输入正确
        userField.onblur=function(){
            var user=userField.value;
            if(/^[a-zA-Z]\w{5,30}$/.test(user)){
                userWarning.innerText='用户名输入正确';
                userWarning.style.color='green';
                console.log('成立')
            }else{
                userWarning.innerText='6-30位字母、数字或"_",字母开头';
                userWarning.style.color='red';
                console.log('不成立')
            }
        }
        //判断密码是否输入正确
        var passwordField=document.getElementById('passwordField');
        var passwordText=document.querySelector('.password-warning-text');
        var passwordWarningBox=document.querySelector('.password-warning-box');
        var passwordWarnings=document.querySelectorAll('.password-warning-box span');

        passwordField.onblur=function(){
            var password=passwordField.value;
            if(/^[a-zA-Z0-9]{6,20}$/.test(password)){
                //验证输入的密码条件
                passwordText.style.display='none';
                passwordWarningBox.style.display='inline-block';
                //判断密码强度等级为一
                if(/^[a-z]{6,20}$|^[A-Z]{6,20}$|^[0-9]{6,20}$/.test(password)){
                    console.log('等级1')
                    passwordWarnings[0].style.background='red';
                }
                //判断密码强度等级为二
                if(/^[a-z][0-9]{6,20}$/.test(password)){
                    console.log('等级2')
                    passwordWarnings[1].style.background='orange';
                }
            }else{
                //密码不符合条件
                passwordText.style.display='inline';
                passwordWarningBox.style.display='none';
            }
        }
    </script>
</body>
</html>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小李 2022-11-30 17:34:28

同学你好,如下仅供参考:

1、判断密码强度为等级2时,其实是检验大写字母与数字组合或者小写字母与数字组合或者大小写字母组合。那么也就是说它们是两两并且的关系如下:

https://img1.sycdn.imooc.com//climg/6387257509ab593130800345.jpg

2、老师这边优化了同学的效果如下:

当密码强度越强时前面的色块依然存在

https://img1.sycdn.imooc.com//climg/63872333097d9ce512390312.jpg

https://img1.sycdn.imooc.com//climg/6387230709bb783613290377.jpg

强度三老师已经写完了如下:

https://img1.sycdn.imooc.com//climg/6387234e093f522513300421.jpg

3、强度三同学尝试按照强度二的思路自己写写试试。

4、这个练习的整体思路是指,第一层校验可以是宽松的,第二层或第三层校验时要把规定“卡死”就没问题了。

祝学习愉快!

  • 提问者 慕函数0445997 #1

    谢谢老师,老师你太棒了,给你点个赞

    2022-12-01 10:00:54
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师