Password强度及验证_js封装

HTMl源码如下:

<head>

    <title>无标题页</title>

    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript" src="JScript.js"></script>

  

</head>

<body>

    <div>

        <ul>

            <li>

                <div class="name_list font_space fleft"><span class="atten">*</span> 密 码:</div>

                <div class="input_list font_space fleft">

                    <input id="password" value="" name="password" size="36" maxlength="36"

                     onblur="this.style.borderColor='#A3BFA8';checkPWD(this.value);fCheckPwd(this.value);"

                     onfocus="this.style.borderColor='#99E300';_( 'chkPwd' ).style.display = 'none';this.style.borderColor='#99E300'"

                     onkeyup="checkPWD(this.value);" type="password" />

                    <div class="error_explain font_space" id="chkPwd" style="display: none;">请输入6~20位字符的密码</div>

                            <!--应用下面的层做密码安全提示

                       

                            最外层的 class 名和 span 内的文字即可调整密码强度呈现,对应关系如:

                            PSW_P_0-----未输入状态

                            PSW_P_1-----太短 (改变span内文字)

                            PSW_P_2-----很弱 (改变span内文字)

                            PSW_P_3-----一般 (改变span内文字)

                            PSW_P_4-----很强 (改变span内文字)

                            -->

                            <div class="PSW_P_0" id="chkpswd">

                                    <div class="PSW_P_W">密码安全程度:<span id="chkpswdcnt">太短</span></div>

                                    <div class="PSW_P_BGC">

                                            <div class="PSW_P_pC"></div>                       

                                    </div>             

                            </div>

                        </div>

                        <div class="explain_list fleft"><span class="explain">密码长度6~20位,由英文字母a~z

                (区分大小写),数字0~9,特殊字符组成。</span></div>

            </li>

            <li>

                <div class="name_list font_space fleft"><span class="atten">*</span> 请再次输入密码:</div>

                <div class="input_list font_space fleft">

                <input name="password_cf" id="password_cf" value="" size="36" maxlength="36"

                 onblur="this.style.borderColor='#A3BFA8';confirmPwd( this )"

                 onfocus="this.style.borderColor='#99E300';_( 'pwdConfirm' ).style.display = 'none';" type="password" />

                <div class="error_explain font_space" id="pwdConfirm" style="display: none;">两次输入的密码不一致</div>

                </div>

            </li>

        </ul>

    </div>

</body>

</html>

 

HTML中用到的JScript.js:

// JScript 文件

function _(id) {//獲取對象

        return document.getElementById(id);

}

function checkPWD( me ){

        _( 'chkPwd' ).style.display = 'none';

        if (me == "") {

                _( "chkpswd" ).className = "PSW_P_0";

                _("chkpswdcnt").innerHTML = "";

                _( 'chkPwd' ).style.display = '';

        } else if (me.length < 6) {

                _( "chkpswd" ).className = "PSW_P_1";

                _("chkpswdcnt").innerHTML = "太短";

        } else if( ! isPassword( me ) || !/^[^%&]*$/.test( me )) {

                _( "chkpswd" ).className = "PSW_P_0";

                _("chkpswdcnt").innerHTML = "";

                _( 'chkPwd' ).style.display = '';

        }

        else {

                var csint = checkStrong(me);

                switch (csint) {

                case 1:

                        _("chkpswdcnt").innerHTML = "很弱";

                        _( "chkpswd" ).className = "PSW_P_"+(csint + 1);

                        break;

                case 2:

                        _("chkpswdcnt").innerHTML = "一般";

                        _( "chkpswd" ).className = "PSW_P_"+(csint + 1);

                        break;

                case 3:        

                        _("chkpswdcnt").innerHTML = "很强";

                        _( "chkpswd" ).className = "PSW_P_"+(csint + 1);

                        break;

                }

        }

}

 

//確定密碼是否合法

function fCheckPwd(str){

    if(!isPassword(str)){

        _( 'chkPwd' ).style.display = '';

    }

}

 

function isPassword( str ){

        if (str.length < 6 || str.length > 16) return false;

        var len;

        var i;

        len = 0;

        for (i=0;i<str.length;i++){

                if (str.charCodeAt(i)>255) return false;

        }

        return true;

}

function CharMode(iN){

        if (iN>=48 && iN <=57) //数字

        return 1;

        if (iN>=65 && iN <=90) //大写字母

        return 2;

        if (iN>=97 && iN <=122) //小写

        return 4;

        else

        return 8; //特殊字符

}

//bitTotal函数

//计算出当前密码当中一共有多少种模式

function bitTotal(num){

        modes=0;

        for (i=0;i<4;i++){

                if (num & 1) modes++;

                num>>>=1;

        }

        return modes;

}

//checkStrong函数

//返回密码的强度级别

function checkStrong(sPW){

        Modes=0;

        for (i=0;i<sPW.length;i++){

                //测试每一个字符的类别并统计一共有多少种模式.

                Modes|=CharMode(sPW.charCodeAt(i));

        }

        return bitTotal(Modes);

}

 

//以下是確定密碼部分

function confirmPwd( me ){

        var oPassword = _( "password" );

        if (oPassword.value == "") {

                return;

        }

        if( ! ( me.value == oPassword.value ) || ! isPassword( me.value ) )

            _( "pwdConfirm" ).style.display = "";

}

HTNML中的StyleSheet.css文件:

 

body,div,ul,li,form,p{padding:0;margin:0;list-style-type:none;}

body{background-color:#FFF;margin-top:7px;}

body,div{text-align:center;font-size:12px;font-family:宋体;margin-left:auto;margin-right:auto;line-height:19px;}

input,select{border:#A3BFA8 1px solid;background-color:#FFF;}

ul,li{clear:both;}

 

.name_list{width:180px;text-align:right; margin-bottom:12px;padding-top:2px; clear:both;}

.font_space{letter-spacing:1px;}

.fleft{float:left;}

.atten{font-size:12px;font-weight:normal;color:#F00;}

.input_list{width:250px;text-align:left;}

 

.explain{color:#A3A3A3;}

.explain_list{background:url(http://mimg.126.com/new/reg/explain.gif) no-repeat;margin-left:0px;padding-left:14px;width:230px;text-align:left;}

/*错误提示*/

.error_explain{ clear:both; text-align:left; padding:0px 50px 5px 0px; color:#FF0000;}

 

/*PSW_P_0-----未输入状态        PSW_P_1-----太短        PSW_P_2-----很弱        PSW_P_3-----一般        PSW_P_4-----很强 */

 

.PSW_P_0,.PSW_P_1,.PSW_P_2,.PSW_P_3,.PSW_P_4{position:relative;height:30px;color:#666}/*密码强度容器*/

        .PSW_P_W{position:absolute;left:0px;top:3px}/*密码强度固定文字*/

        .PSW_P_BGC{position:absolute;left:0px;top:22px;width:235px!important;width:234px;height:4px;background-color:#E0E0E0; font-size:0px;line-height:0px}/*灰色强度背景*/

        .PSW_P_pC{float:left;font-size:0px;line-height:0px;height:4px}/*色块背景*/

       

        .PSW_P_0 span{display:none}

        .PSW_P_1 span{display:inline;color:#F00}

        .PSW_P_2 span{display:inline;color:#C48002}

        .PSW_P_3 span{display:inline;color:#2CA4DE}

        .PSW_P_4 span{display:inline;color:#063}

       

        .PSW_P_0 .PSW_P_pC{ width:0px}

        .PSW_P_1 .PSW_P_pC{ width:25%;background-color:#F00}

        .PSW_P_2 .PSW_P_pC{ width:50%;background-color:#F90}

        .PSW_P_3 .PSW_P_pC{ width:75%;background-color:#2CA4DE}

        .PSW_P_4 .PSW_P_pC{ width:100%;background-color:#063}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值