jquery用正则表达式验证密码强度

时间:2023-03-08 16:35:02
/**
         * 不加paste鼠标粘贴不起作用
         * 不加input第一次粘贴的时候不变
         * 加上input和focus可以兼容表情
         * keydown兼容IE6下按键不放
         */
        $('#email_pass').bind("keyup paste focus keydown",function(e) {
            //(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$ 强度密码正则(可选)
            var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
            var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
            var enoughRegex = new RegExp("(?=.{6,}).*", "g");
            if (false == enoughRegex.test($(this).val())) {
                $('#strong').css('background-color','#FF6600');
                $('#good').css('background-color','#FF6600');
                $('#weak').css('background-color','#FF6600');
            } else if (strongRegex.test($(this).val())) {
                $('#strong').css('backgroundColor','green');
                $('#good').css('backgroundColor','green');
                $('#weak').css('background-color','green');
            } else if (mediumRegex.test($(this).val())) {
                $('#good').css('backgroundColor','green');
                $('#weak').css('background-color','green');
                $('#strong').css('background-color','#FF6600');
            } else {
                $('#weak').css('background-color','green');
                $('#strong').css('background-color','#FF6600');
                $('#good').css('background-color','#FF6600');
            }
            return true;
        }); <input type="password" name="pass" id="email_pass" />
<span id="passstrength"></span>