美文网首页
js_5 密码强度

js_5 密码强度

作者: basicGeek | 来源:发表于2018-11-20 18:06 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<style type="text/css">
    body {
        background: #ccc;
    }

    label {
        width: 40px;
        display: inline-block;
    }

    span {
        color: red;
    }

    .container {
        margin: 100px auto;
        width: 400px;
        padding: 50px;
        line-height: 40px;
        border: 1px solid #999;
        background: #efefef;
    }

    span {
        margin-left: 30px;
        font-size: 12px;
    }

    .wrong {
        color: red
    }

    .right {
        color: green;
    }

    .strengthLv0 {
        height: 6px;
        width: 120px;
        border: 1px solid #ccc;
        padding: 2px;
    }

    .strengthLv1 {
        background: red;
        height: 6px;
        width: 40px;
        border: 1px solid #ccc;
        padding: 2px;
    }

    .strengthLv2 {
        background: orange;
        height: 6px;
        width: 80px;
        border: 1px solid #ccc;
        padding: 2px;
    }

    .strengthLv3 {
        background: green;
        height: 6px;
        width: 120px;
        border: 1px solid #ccc;
        padding: 2px;
    }
</style>
<body>
<div class="container">
    <label>密码</label>
    <input type="text" id="inp1" maxlength="16">
    <!--<input type="password" id="inp1" maxlength="16"/>-->
    <div class="pass-wrap">
        <em>密码强度:</em>

        <em id="strength"></em>

        <div id="strengthLevel" class="strengthLv0"></div>
    </div>
</div>
<script>
    var inp1 = document.getElementById("inp1");
    var strength = document.getElementById("strength");
    var strengthLevel = document.getElementById("strengthLevel");
    var array = ['', '低', '中', '高'];
    inp1.onkeyup = function () {
        var level = 0;
        if (/[a-z]/.test(this.value)) {
            level++;
        }
        if (/[0-9]/.test(this.value)) {
            level++;
        }
        if (/[^a-z0-9]/.test(this.value)) {
            level++;
        }
        if (this.value.length < 6) {
            level = 0;
        }
        if (level > 3) {
            level = 3;
        }
        strength.innerHTML = array[level];
        strengthLevel.className = 'strengthLv' + level;
    };
</script>
</body>
</html>
密码强度密码强度

相关文章

网友评论

      本文标题:js_5 密码强度

      本文链接:https://www.haomeiwen.com/subject/dswxqqtx.html