JavaScriptでパスワード強度をチェックするサンプルプログラム
JavaScriptコード:
<script language="javascript">
function passwordChanged() {
var strength = document.getElementById('strength’);
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");
var pwd = document.getElementById(“password");
if (pwd.value.length == 0) {
strength.innerHTML = 'Type Password’;
} else if (false == enoughRegex.test(pwd.value)) {
strength.innerHTML = 'More Characters’;
} else if (strongRegex.test(pwd.value)) {
strength.innerHTML = ' <span style = “color:green">強い! </span>’;
} else if (mediumRegex.test(pwd.value)) {
strength.innerHTML = '<span style="color:orange">まあまあ!</span> ';
} else {
strength.innerHTML = ' <span style = “color:red"> 弱い! </span>’;
}
}
</script>