js調用此函數,返回密碼強度層級
function getStrength(passwd){ intScore = 0; if (passwd.match(/[a-z]/)) // [驗證]至少一個小寫字母 { intScore = (intScore+1) } if (passwd.match(/[A-Z]/)) // [驗證]至少一個大寫字母 { intScore = (intScore+5) } // 單一驗證 if (passwd.match(//d+/)) // [驗證]至少一個數字 { intScore = (intScore+5) } if (passwd.match(/(/d.*/d.*/d)/)) // [驗證]至少三個數字 { intScore = (intScore+5) } // 特殊字元驗證 if (passwd.match(/[!,@#$%^&*?_~]/)) // [驗證]至少一個特殊字元 { intScore = (intScore+5) } if (passwd.match(/([!,@#$%^&*?_~].*[!,@#$%^&*?_~])/)) // [驗證]至少兩個特殊字元 { intScore = (intScore+5) } // 複合驗證 if (passwd.match(/[a-z]/) && passwd.match(/[A-Z]/)) // [驗證]同時包含大寫和小寫 { intScore = (intScore+2) } if (passwd.match(//d/) && passwd.match(//D/)) // [驗證] 同時包含字母和數字 { intScore = (intScore+2) } // [驗證] 同時包含大寫字母,小寫字母,數字和特殊字元 if (passwd.match(/[a-z]/) && passwd.match(/[A-Z]/) && passwd.match(//d/) && passwd.match(/[!,@#$%^&*?_~]/)) { intScore = (intScore+2) } return intScore; } function getStrength(passwd){ intScore = 0; if (passwd.match(/[a-z]/)) // [驗證]至少一個小寫字母 { intScore = (intScore+1) } if (passwd.match(/[A-Z]/)) // [驗證]至少一個大寫字母 { intScore = (intScore+5) } // 單一驗證 if (passwd.match(//d+/)) // [驗證]至少一個數字 { intScore = (intScore+5) } if (passwd.match(/(/d.*/d.*/d)/)) // [驗證]至少三個數字 { intScore = (intScore+5) } // 特殊字元驗證 if (passwd.match(/[!,@#$%^&*?_~]/)) // [驗證]至少一個特殊字元 { intScore = (intScore+5) } if (passwd.match(/([!,@#$%^&*?_~].*[!,@#$%^&*?_~])/)) // [驗證]至少兩個特殊字元 { intScore = (intScore+5) } // 複合驗證 if (passwd.match(/[a-z]/) && passwd.match(/[A-Z]/)) // [驗證]同時包含大寫和小寫 { intScore = (intScore+2) } if (passwd.match(//d/) && passwd.match(//D/)) // [驗證] 同時包含字母和數字 { intScore = (intScore+2) } // [驗證] 同時包含大寫字母,小寫字母,數字和特殊字元 if (passwd.match(/[a-z]/) && passwd.match(/[A-Z]/) && passwd.match(//d/) && passwd.match(/[!,@#$%^&*?_~]/)) { intScore = (intScore+2) } return intScore;
}另外一種方法。用一條正則驗證密碼強度,密碼為6位元字或字元組成,且必須含有一個數字、一個字元
^(?=\d{0,5}[a-zA-Z])(?=[a-zA-Z]{0,5}\d)[a-zA-Z0-9]{6}$ ^(?=\d{0,5}[a-zA-Z])(?=[a-zA-Z]{0,5}\d)[a-zA-Z0-9]{6}$1.一個 ^[a-zA-Z0-9]{6}$ 匹配整個密碼串
2.接著1個斷言,驗證密碼中必須出現過字元 (?=\d{0,5}[a-zA-Z])
3.類似第2步,也是一個斷言,驗證密碼中必須出現過數字 (?=[a-zA-Z]{0,5}\d)