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