首页 > 其他 > 详细

密码强度小练习

时间:2014-11-03 16:08:20      阅读:211      评论:0      收藏:0      [点我收藏+]

body里面

bubuko.com,布布扣
<body>
    <input id="txt" type="text" name="name" value="" style="width: 300px; border: 1px solid gray;" />
    <table id="tb" style="width: 300px; text-align: center; background-color: #E6E6E6;">
        <tr>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </table>
</body>
View Code

<script>标签里面

bubuko.com,布布扣
 <script type="text/javascript">

        window.onload = function () {

            document.getElementById(‘txt‘).onkeyup = function () {

                var tds = document.getElementById(‘tb‘).getElementsByTagName(‘td‘);
                for (var i = 0; i < tds.length; i++) {
                    tds[i].style.backgroundColor = ‘#E6E6E6‘;
                }
                //获取密码
                var pwdTxt = this.value;
                //调方法
                if (pwdTxt.length > 0) {

                    var num = checkPwd(pwdTxt);
                    if (num <= 1) {
                       tds[0].style.backgroundColor = ‘green‘;
                    } else if (num == 2) {
                       tds[0].style.backgroundColor = ‘red‘;
                       tds[1].style.backgroundColor = ‘red‘;
                    } else if (num == 3) {
                       tds[0].style.backgroundColor = ‘blue‘;
                       tds[1].style.backgroundColor = ‘blue‘;
                       tds[2].style.backgroundColor = ‘blue‘;
                    }
                }
                //判断等级
            };
        };


        //判断密码的等级
        function checkPwd(pwd) {
            var lvl = 0; //存等级的
            if (pwd.match(/\d/)) {//判断是否包含数字
                lvl++;
            }
            if (pwd.match(/[a-zA-Z]/)) {
                lvl++;
            }
            if (pwd.match(/[^0-9a-zA-Z]/)) {
                lvl++;
            }
            if (pwd.length <= 6) {
                lvl--;
            }
            return lvl;
        }
    </script>
View Code

效果bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

密码强度小练习

原文:http://www.cnblogs.com/valiant1882331/p/4071360.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!