顯示密碼安全性強度

<div class="from-nav">
                        
<div class="Mtitle"><span>密码:</span></div>
                        
<div class="Mright"><input name="password" type="password" id="password" size="20" class="colorblue" onfocus="this.className='colorfocus';" onblur="this.className='colorblue';" onkeyup="return loadinputcontext(this);" /><span>不得少于6个字符</span>
                        
</div>
    </div>
    <div class="from-nav">
                        
<div class="Mtitle"><span>密码强度:</span></div>
                        
<div class="Mright">
<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;
        }

        
function htmlEncode(source, display, tabs)
        
{
            
function special(source)
            
{
                
var result = '';
                
for (var i = 0; i < source.length; i++)
                
{
                    
var c = source.charAt(i);
                    
if (c < ' ' || c > '~')
                    
{
                        c 
= '&#' + c.charCodeAt() + ';';
                    }

                    result 
+= c;
                }

                
return result;
            }

            
function format(source)
            
{
                
// Use only integer part of tabs, and default to 4
                tabs = (tabs >= 0? Math.floor(tabs) : 4;
                
// split along line breaks
                var lines = source.split(/\r\n|\r|\n/);
                
// expand tabs
                for (var i = 0; i < lines.length; i++)
                
{
                    
var line = lines[i];
                    
var newLine = '';
                    
for (var p = 0; p < line.length; p++)
                    
{
                        
var c = line.charAt(p);
                        
if (c === '\t')
                        
{
                            
var spaces = tabs - (newLine.length % tabs);
                            
for (var s = 0; s < spaces; s++)
                            
{
                                newLine 
+= ' ';
                            }

                        }

                        
else
                        
{
                            newLine 
+= c;
                        }

                    }

                    
// If a line starts or ends with a space, it evaporates in html
                    // unless it's an nbsp.
                    newLine = newLine.replace(/(^ )|( $)/g, '&nbsp;');
                    lines[i] 
= newLine;
                }

                
// re-join lines
                var result = lines.join('<br />');
                
// break up contiguous blocks of spaces with non-breaking spaces
                result = result.replace(/  /g, ' &nbsp;');
                // tada!
                return result;
            }

            
var result = source;
            
// ampersands (&)
            result = result.replace(/\&/g,'&amp;');
            
// less-thans (<)
            result = result.replace(/\</g,'&lt;');
            
// greater-thans (>)
            result = result.replace(/\>/g,'&gt;');
            
if (display)
            
{
                
// format for display
                result = format(result);
            }

            
else
            
{
                
// Replace quotes if it isn't for display,
                // since it's probably going in an html attribute.
                result = result.replace(new RegExp('"','g'), '&quot;');
            }

            
// special characters
            result = special(result);
            
// tada!
            return result;
        }

        
var profile_username_toolong = '对不起,您的用户名超过 20 个字符,请输入一个较短的用户名。';
        
var profile_username_tooshort = '对不起,您输入的用户名小于3个字符, 请输入一个较长的用户名。';
        
var profile_username_pass = "可用";
        
function checkusername(username)
        
{
            
var unlen = username.replace(/[^\x00-\xff]/g, "**").length;
            
if(unlen < 3 || unlen > 20{
                document.getElementById(
"checkresult").innerHTML = "<font color='#009900'>" + (unlen < 3 ? profile_username_tooshort : profile_username_toolong) + "</font>";
                
return;
            }

            ajaxRead(
"tools/ajax.aspx?t=checkusername&username=" + escape(username), "showcheckresult(obj,'" + username + "');");
        }

        
function showcheckresult(obj, username)
        
{
            
var res = obj.getElementsByTagName('result');
            
var resContainer = document.getElementById("checkresult");
            
var result = "";
            
if (res[0!= null && res[0!= undefined)
            
{
                
if (res[0].childNodes.length > 1{
                    result 
= res[0].childNodes[1].nodeValue;
                }
 else {
                    result 
= res[0].firstChild.nodeValue;            
                }

            }

            
if (result == "1")
            
{
                resContainer.innerHTML 
= "<font color='#009900'>对不起,您输入的用户名 \"" + htmlEncode(username, true, 4) + "\" 已经被他人使用或被禁用,请选择其他名字后再试。</font>";
            }

            
else
            
{
                resContainer.innerHTML 
= profile_username_pass;
            }

        }

</script>
<script type="text/javascript" src="templates/default/ajax.js"></script>
             <span id="showmsg"></span>
                </div>
    </div>
posted @ 2008-02-18 10:06  Nina  阅读(359)  评论(0编辑  收藏  举报