javascript 密碼強度驗證程式代碼

來源:互聯網
上載者:User

我們先來看強度規則

1) 任何少於6個字元的組合,弱;任何字元數的同類字元組合,弱;       

2) 任何字元數的兩類字元組合,中;       

3) 12位字元數以下的三類或四類字元組合,強;       

4) 12位字元數以上的三類或四類字元組合,非常好。

 代碼如下 複製代碼

<head>   

<title>密碼強度檢測</title>   

<style type="text/css">       

body { font: 12px/1.5 Arial; }       

input{  float: left;font-size: 12px; width: 150px;font-family: arial; border: 1px solid #ccc; padding: 3px;}       

input.correct{ border: 1px solid green; }       

input.error{border: 1px solid red;}       

#tips{float: left; margin: 2px 0 0 20px; }       

#tips span {float: left;  width: 50px; height: 20px; color: #fff;overflow: hidden;background: #ccc;margin-right: 2px;line-height: 20px; text-align: center;}       

#tips.s1 .active { background: #f30; }       

#tips.s2 .active {background: #fc0; }       

#tips.s3 .active {background: #cc0;}       

#tips.s4 .active  {background: #090; }   

</style>   

<script type="text/javascript">       

window.onload = function () {           

var oTips = document.getElementById("tips");           

var oInput = document.getElementsByTagName("input")[0];           

var aSpan = oTips.getElementsByTagName("span");           

var aStr = ["弱", "中", "強", "非常好"];           

var i = 0;

oInput.onkeyup = oInput.onfocus = oInput.onblur = function () {               

var index = checkStrong(this.value);               

this.className = index ? "correct" : "error";               

oTips.className = "s" + index;               

for (i = 0; i < aSpan.length; i++) aSpan[i].className = aSpan[i].innerHTML = "";               

index && (aSpan[index - 1].className = "active", aSpan[index - 1].innerHTML = aStr[index - 1])             }         };       

 

//檢測密碼強度       

function checkStrong(sValue) {           

var modes = 0;           

if (sValue.length < 6) return modes;           

if (/d/.test(sValue)) modes++; //數字           

if (/[a-z]/.test(sValue)) modes++; //小寫           

if (/[A-Z]/.test(sValue)) modes++; //大寫            

if (/W/.test(sValue)) modes++; //特殊字元           

switch (modes) {               

case 1:                   

return 1;                   

break;               

case 2:                   

return 2;               

case 3:               

case 4:                   

return sValue.length < 12 ? 3 : 4                   

break;             }         }   

</script>

</head>

<body>   

<input type="password" value="" maxlength="16" />     <div id="tips">       

<span></span><span></span><span></span><span></span>   

</div>

</body>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.