javascript密碼強度測試

來源:互聯網
上載者:User
<script type="text/javascript">
        var PasswordStrength ={
            Level : ["極佳","一般","較弱","太短"],//強度提示資訊
            LevelValue : [15,10,5,0],//和上面提示資訊對應的強度值
            Factor : [1,2,5],//強度加權數,分別為字母,數字,其它
            KindFactor : [0,0,10,20],//密碼含幾種組成的權數
            Regex : [/[a-zA-Z]/g,/\d/g,/[^a-zA-Z0-9]/g] //字元,數字,非字元數字(即特殊符號)
            }
        PasswordStrength.StrengthValue = function(pwd)
        {
            var strengthValue = 0;
            var ComposedKind = 0;
            for(var i = 0 ; i < this.Regex.length;i++)
            {
                var chars = pwd.match(this.Regex[i]);//匹配當前密碼中符合指定正則的字元,如果有多個字元以','分隔
                if(chars != null)
                {
                    strengthValue += chars.length * this.Factor[i];
                    ComposedKind ++;
                }
            }
            strengthValue += this.KindFactor[ComposedKind];
            return strengthValue;
        } 
        PasswordStrength.StrengthLevel = function(pwd)
        {
            var value = this.StrengthValue(pwd);
            for(var i = 0 ; i < this.LevelValue.length ; i ++)
            {
                if(value >= this.LevelValue[i] )
                    return this.Level[i];
            }
        }
    function loadinputcontext(o)
    {
        var showmsg=PasswordStrength.StrengthLevel(o.value);
        switch(showmsg)
        {
        case "太短": showmsg+=" <img src='images/level/1.gif' width='88' height='11'
/>";
        break;
        case "較弱": showmsg+=" <img src='images/level/2.gif' width='88' height='11'
/>";
        break;
        case "一般": showmsg+=" <img src='images/level/3.gif' width='88' height='11'
/>";
        break;
        case "極佳": showmsg+=" <img src='images/level/4.gif' width='88' height='11'
/>";
        break;
         }
         document.getElementById('showmsg').innerHTML = showmsg;
    }
</script>

<input name="password" type="password" id="password" size="20" onkeyup="return loadinputcontext(this);" />
<span id="showmsg"></span>

相關文章

聯繫我們

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