asp.net用户注册的与密码强度及验证的相关问题。对上一次详细叙述。

1.近期学习的用户注册是密码强度问题。


<script type="text/javascript" language="javascript">
        //总:JS判断密码强度

        //判断输入密码的类型 测试某个字符是属于哪一类. 
        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;
        }
        //返回强度级别  
        function checkStrong(sPW) {
            if (sPW.length < 5)
                return 0; //密码太短,不检测级别
            Modes = 0;
            for (i = 0; i < sPW.length; i++) {
                //密码模式  测试每一个字符的类别并统计一共有多少种模式. 与或运算.
                Modes |= CharMode(sPW.charCodeAt(i));
            }
            return bitTotal(Modes);
        }
        //1.如果密码少于5位,那么就认为这是一个弱密码;
        //2.如果密码只由数字、小写字母、大写字母或其它特殊符号当中的一种组成,则认为这是一个弱密码.
        //3.如果密码由数字、小写字母、大写字母或其它特殊符号当中的两种组成,则认为这是一个中度安全的密码.
        //4.如果密码由数字、小写字母、大写字母或其它特殊符号当中的三种以上组成,则认为这是一个比较安全的密码.

        //显示颜色-当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色 
        function pwStrength(pwd) {
            Dfault_color = "#eeeeee";		//默认颜色
            L_color = "#FF0000";		//低强度的颜色,且只显示在最左边的单元格中
            M_color = "#FF9900";		//中等强度的颜色,且只显示在左边两个单元格中
            H_color = "#33CC00";		//高强度的颜色,三个单元格都显示
            if (pwd == null || pwd == '') {
                Lcolor = Mcolor = Hcolor = Dfault_color;
            }
            else {
                S_level = checkStrong(pwd);
                switch (S_level) {
                    case 0:
                        Lcolor = Mcolor = Hcolor = Dfault_color;
                        break;
                    case 1:
                        Lcolor = L_color;
                        Mcolor = Hcolor = Dfault_color;
                        break;
                    case 2:
                        Lcolor = Mcolor = M_color;
                        Hcolor = Dfault_color;
                        break;
                    default:
                        Lcolor = Mcolor = Hcolor = H_color;
                }
            }
            document.getElementById("strength_L").style.background = Lcolor;
            document.getElementById("strength_M").style.background = Mcolor;
            document.getElementById("strength_H").style.background = Hcolor;
            return;
        }
        //document.getElementById  :根据指定的 id 属性值得到对象。
        //document.getElementById(" ") 得到的是一个对象,
        //用 alert 显示得到的是“ object ”,而不是具体的值,它有 value 和 length 等属性,加上 .value 得到的才是具体的值!

    </script>


在前台的控件中注意加入控制属性:


代码如下:

<asp:TextBox ID="txtPass" runat="server" onFocus="tPass();" 
    TextMode="Password" Width="115px" onKeyUp="pwStrength(this.value)" onBlur="pwStrength(this.value)" ></asp:TextBox><span class="marginleft">*</span></td>
      <td style="width:230px;" align="left">
       <table id="tab" border="0"align="left" cellpadding="0" cellspacing="2" bordercolor="#eeeeee" width="90%">
           <tr align="center" >
		<td width="40%">密码强度:</td>
		<td width="20%" id="strength_L" >弱</td>  
		<td width="20%" id="strength_M" >中</td>  
		<td width="20%" id="strength_H" >强</td>  
	    </tr>


网络上还有不同的样式:结果是大同小异的。

代码如下:

<div>      
       输入密码:<asp:TextBox runat="server" onKeyUp=pwStrength(this.value) onBlur=pwStrength(this.value) ></asp:TextBox><br />      
       密码强度:      
       <table border="1" cellpadding="1" borderColorDark="#fdfeff" borderColorLight="#99ccff" cellspacing="1" style="width: 200px; display: inline; background-color:#e0f0ff">      
           <tr>      
               <td style="width: 100px; height: 19px;" align="center">      
                   弱</td>      
               <td style="width: 100px; height: 19px;" align="center">      
                   中</td>      
               <td style="width: 100px; height: 19px;" align="center">      
                   强</td>      
           </tr>      
       </table>      
   </div>  



2.js脚本验证手机号邮箱等问题所使用的方法

代码如下:
<script type="text/javascript">
        function PassIsSame() {
            var pwd1 = document.getElementById("<%= txtPass.ClientID %>").value;
            var pwd2 = document.getElementById("<%= txtQpass.ClientID %>").value;//<!-- 对比两次输入的密码 -->
            if (pwd1 == pwd2) {
                document.getElementById("PassTishi").innerHTML = "";
            }
            else {
                document.getElementById("PassTishi").innerHTML = "<font color='red'>两次密码不相同</font>";
            }
            return;
        }


        //验证手机号码 
        function checkPhone() {  
            var phone = document.getElementById("<%= txtPhone.ClientID %>").value;
            if (!(/^1[34578]\d{9}$/.test(phone))) {
                document.getElementById("tPhoneTishi").innerHTML = "<font color='red'>手机号码验证出错!</font>";
                return false;
            }
            else
            {
                document.getElementById("tPhoneTishi").innerHTML = "";
                return true;
            }
        }


        //验证邮箱格式 
        function checktxtEmail() {
            var phone = document.getElementById("<%= txtEmail.ClientID %>").value;
            if (!(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(phone))) {
                document.getElementById("txtEmailTishi").innerHTML = "<font color='red'>邮箱格式验证出错!</font>";
                return false;
            }
            else {
                document.getElementById("txtEmailTishi").innerHTML = "";
                return true;
            }
        }
    </script>


3.将鼠标放到textbox文本框中所出现的对应的提示

代码:
<script type="text/javascript">
	//显示会员名输入提示
function tName()
{
	       document.getElementById("sp").innerHTML="只能输入数字、字母下划线,<br>例如:mr_2008";	       
	    }
	  //显示密码输入提示  
	 function tPass()
	    {
	         document.getElementById("sp").innerHTML="为了提供密码的安全性。<br>建议密码在6位以上。";
	 }
	 //显示密码输入提示  
	 function tQPass() {
	     document.getElementById("sp").innerHTML = "再次输入密码。<br>建议密码在6位以上。";
	 }
	   
	  //显示电话码输入提示
	 function tPhone()
	 {
	        document.getElementById("sp").innerHTML="输入手机号,以方便联系您<br>手机号应为11位";
	 }
	 //显示电子邮件输入提示
	function tEmail()
	{
	        document.getElementById("sp").innerHTML="请输入正确的电子邮件。<br>例如:[email protected]";
	}
	//显示所在城市输入提示
	function tCity()
	{
	    document.getElementById("sp").innerHTML="输入所在城市。<br>例如:长春市";
	}
	//显示找回密码问题提示
	function tQuestion() {
	    document.getElementById("sp").innerHTML = "输入找回密码问题。<br>例如:我的小学名称";
	}
        //显示找回密码问题答案提示
	function tQuestionAnswer() {
	    document.getElementById("sp").innerHTML = "输入找回密码问题答案。<br>例如:中所屯小学";
	}
    </script>


<span id="sp" style="font-size: 14px; color: BlueViolet;"></span>

在前台textbox控件下所需要做的操作

代码
 <span >确认密码:</span>
 <td style="text-align: left">
<asp:TextBox ID="txtQpass" runat="server" TextMode="Password" Width="115px" onFocus="tQPass();" onKeyUp="PassIsSame();"></asp:TextBox><span
class="marginleft">*</span>
<div style="width: 100%;"><span id="PassTishi" style="font-size: 12px; color: red;"></span></div>
<td style="text-align: right;">

<span >电  话:</span>
<td style="text-align: left;">
<asp:TextBox onFocus="tPhone();" ID="txtPhone" runat="server" Width="115px" onKeyUp="checkPhone();"></asp:TextBox></td>

<div style="width: 100%;"><span id="tPhoneTishi" style="font-size: 12px; color: red;"></span></div>



<td style="text-align: right; ">
<span >E-mail:</span>
<td style="width: 13px; text-align: left; ">
<asp:TextBox ID="txtEmail" onFocus="tEmail();" runat="server" Width="115px" onKeyUp="checktxtEmail();"></asp:TextBox></td>
<div style="width: 100%;"><span id="txtEmailTishi" style="font-size: 12px; color: red;"></span></div>

<td style="text-align: right;">
<span >所在城市:</span></td>
<td style="text-align: left">
<asp:TextBox ID="txtCity" onFocus="tCity();" runat="server" Width="115px"></asp:TextBox></td>

<td style="width:150px; text-align: right; font-size:13px;">
<span >找回密码时的问题:</span>
<td style="text-align: left" colspan="2">
<asp:TextBox ID="txt_Question" onFocus="tQuestion();" runat="server" Width="235px"></asp:TextBox></td>

<td style=" text-align: right;">
 <span >问题答案:</span>
<td style="text-align: left" colspan="2">
<asp:TextBox ID="txt_Answer" onFocus="tQuestionAnswer();" runat="server" Width="235px"></asp:TextBox></td>




4.用Md5加密得到的密码项数据库中记录

代码:
 string userPass = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPass.Text, "MD5");



附加。

猜你喜欢

转载自blog.csdn.net/Lsc_hei/article/details/78675767