密码强度检测

function teststrength(password){
    var option = {
        'shortPass': '短',
        'badPass': '弱',
        'goodPass': '好',
        'strongPass': '强'
    };
    this.resultStyle = '';
    var score = 0;
    
    
    if (password.length < 4) {
        this.resultStyle = option.shortPass;
        return {
            'level': option.shortPass,
            'score': score
        };
    }
    
    score += password.length * 4;
    score += (checkRepetition(1, password).length - password.length) * 1;
    score += (checkRepetition(2, password).length - password.length) * 1;
    score += (checkRepetition(3, password).length - password.length) * 1;
    score += (checkRepetition(4, password).length - password.length) * 1;
    
    
    if (password.match(/(.*[0-9].*[0-9].*[0-9])/)) {
        score += 5;
    }
    
    
    if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) {
        score += 5;
    }
    
    
    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) {
        score += 10;
    }
    
    
    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) {
        score += 15;
    }
    
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/)) {
        score += 15;
    }
    
    
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/)) {
        score += 15;
    }
    
    
    if (password.match(/^\w+$/) || password.match(/^\d+$/)) {
        score -= 10;
    }
    if (score < 0) {
        score = 0;
    }
    if (score > 100) {
        score = 100;
    }
    
    if (score < 34) {
        this.resultStyle = option.badPass;
        return {
            'level': option.badPass,
            'score': score
        };
    }
    if (score < 68) {
        this.resultStyle = option.goodPass;
        return {
            'level': option.goodPass,
            'score': score
        };
    }
    
    this.resultStyle = option.strongPass;
    return {
        'level': option.strongPass,
        'score': score
    };
}

function checkRepetition(pLen, str){
    var res = "";
    for (var i = 0; i < str.length; i++) {
        var repeated = true;
        
        for (var j = 0; j < pLen && (j + i + pLen) < str.length; j++) {
            repeated = repeated && (str.charAt(j + i) == str.charAt(j + i + pLen));
        }
        if (j < pLen) {
            repeated = false;
        }
        if (repeated) {
            i += pLen - 1;
            repeated = false;
        }
        else {
            res += str.charAt(i);
            
        }
    }
    return res;
};

 抄自jquery password strength plugin

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值