js使用者密碼強度驗證函式

來源:互聯網
上載者:User

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)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.