<form id="profile">
<input id="pass1" type="text" />
<input id="pass2" type="text" /> <a class="button">Strong Pass</a>
</form>
<div id="pass-strength-result" class="alert alert-info" style="width:169px">Password line</div>
Jクエリ
// MY CODE START HERE
var keylist="abcdefghijklmnopqrstuvwxyz123456789"
var temp=''
function generatepass(plength){
temp=''
for (i=0;i<plength;i++)
temp+=keylist.charAt(Math.floor(Math.random()*keylist.length))
return temp
}
$(document).on('click','.button',function(){
$('#profile #pass1,#profile #pass2').val(generatepass(8));
check_pass_strength();
});
/*********/
function check_pass_strength () {
var pass = $('#pass1').val();
var pass2 = $('#pass2').val();
var user = 'admin';
$('#pass-strength-result').removeClass('short bad good strong');
if ( ! pass ) {
$('#pass-strength-result').html( pwsL10n.empty );
return;
}
var strength = passwordStrength(pass, user, pass2);
$('#pass-strength-result').removeClass('good strong danger bad');
if ( 2 == strength )
$('#pass-strength-result').addClass('bad').html( pwsL10n.bad );
else if ( 3 == strength )
$('#pass-strength-result').addClass('good').html( pwsL10n.good );
else if ( 4 == strength )
$('#pass-strength-result').addClass('strong').html( pwsL10n.strong );
else if ( 5 == strength )
$('#pass-strength-result').addClass('danger').html( pwsL10n.mismatch );
else
$('#pass-strength-result').addClass('danger').html( pwsL10n.short );
}
/*$(document).on('input','#pass1, #pass2',function( {check_pass_strength();});*/
$('#pass1, #pass2').val('').keyup( check_pass_strength ).change(check_pass_strength);
pwsL10n = {
empty: "Password line",
short: "Very easy",
bad: "Easy",
good: "Medium",
strong: "High",
mismatch: "Different Password"
}
try{convertEntities(pwsL10n);}catch(e){};
//------------------- Another include (Plug-in)------------------
// Password strength meter
function passwordStrength(password1, username, password2) {
var shortPass = 1, badPass = 2, goodPass = 3, strongPass = 4, mismatch = 5, symbolSize = 0, natLog, score;
// password 1 != password 2
if ( (password1 != password2) && password2.length > 0)
return mismatch
//password < 4
if ( password1.length < 4 )
return shortPass
//password1 == username
if ( password1.toLowerCase() == username.toLowerCase() )
return badPass;
if ( password1.match(/[0-9]/) )
symbolSize +=10;
if ( password1.match(/[a-z]/) )
symbolSize +=26;
if ( password1.match(/[A-Z]/) )
symbolSize +=26;
if ( password1.match(/[^a-zA-Z0-9]/) )
symbolSize +=31;
natLog = Math.log( Math.pow(symbolSize, password1.length) );
score = natLog / Math.LN2;
if (score < 40 )
return badPass
if (score < 56 )
return goodPass
return strongPass;
}
デモ: http://jsfiddle.net/iOnur/rqDdp/