我在表单上有以下脚本用于输入密码:
$('#genPass').click(function() {
var password = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 12; i++)
password += possible.charAt(Math.floor(Math.random() * possible.length));
$('input[name="password"]').val(password);
document.getElementById("str").innerHTML = password;
});
function passwordStrength(password)
{
var desc = new Array();
desc[0] = "Very Weak";
desc[1] = "Weak";
desc[2] = "Better";
desc[3] = "Medium";
desc[4] = "Strong";
desc[5] = "Strongest";
var score = 0;
//if password bigger than 6 give 1 point
if (password.length > 6) score++;
//if password has both lower and uppercase characters give 1 point
if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;
//if password has at least one number give 1 point
if (password.match(/\d+/)) score++;
//if password has at least one special caracther give 1 point
if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) ) score++;
//if password bigger than 12 give another 1 point
if (password.length > 12) score++;
document.getElementById("passwordStrength").innerHTML = desc[score];
document.getElementById("passwordStrength").className = "strength" + score;
}
输入字段和生成密码按钮:
<input name="password" type="password" size="25" maxlength="20" id="pass" onchange="passwordStrength(this.value)" onselect="passwordStrength(this.value)" onclick="passwordStrength(this.value)"/>
<button id="genPass" type="button">Generate Password !</button>
现在,一旦您单击一个按钮,它就会在密码输入中生成一个密码,但只有在您单击密码输入字段时,才会显示通过强度。我希望它在您单击生成按钮时显示强度,有什么想法吗?