DoNet用Ajax实现对修改密码时输入数据合法性的验证

1.前台页面(Default.aspx):

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>修改密码</title>
    <script type="text/javascript" src="JS/jquery-1.4.2.min.js"></script><%--添加对JQuery类库的引用--%>
    <script type="text/javascript">
var oldPwdPass=false;
var newPwdPass=false;
var newPwd;
function checkOldPwd(pwd)
{
   if(pwd==""||pwd==null)
   {
      document.getElementById("get_old_passwordMsg").innerHTML="旧密码不能为空!";
      return false;
   }
   else if(validatePwd(pwd))
   {
      document.getElementById("get_old_passwordMsg").innerHTML="旧密码只能是6-16位的字母或数字!";
      return false;
   }
   else
   {
 
      document.getElementById("get_old_passwordMsg").innerHTML="";
      //下面自动调用引用的JQuery类库
      $.ajax({
                 type:"get",
                 url:"CheckPwd.ashx",//转到一般处理程序
                 data:{"Pwd":pwd,"random":Math.random()},              
                 success:function(data)
                 {              
                     if(data=="true")
                     {
                        oldPwdPass=true;
                        return true;
                     }
                     else
                     {
                        document.getElementById("get_old_passwordMsg").innerHTML="旧密码输入错误!";
                        document.getElementById("get_old_password").value="";
                        return false;
                     }      
                 }                    
         });
   }                 
}

function checkNewPwd(pwd)
{
   if(pwd==""||pwd==null)
   {
      document.getElementById("get_new_passwordMsg").innerHTML="新密码不能为空!";
      return false;
   }
   else if(validatePwd(pwd))
   {
      document.getElementById("get_new_passwordMsg").innerHTML="新密码只能是6-16位的字母或数字!";
      return false;
   }
   else
   {
      newPwd=pwd;
      document.getElementById("get_new_passwordMsg").innerHTML="";
      return true;
   }   
}

function checkConfirmNewPwd(pwd)
{
   if(pwd==""||pwd==null)
   {
      document.getElementById("confirm_new_passwordMsg").innerHTML="确认新密码不能为空!";
      return false;
   }
   else if(validatePwd(pwd))
   {
      document.getElementById("confirm_new_passwordMsg").innerHTML="确认新密码只能是6-16位的字母或数字!";
      return false;
   }
   else
   {
      document.getElementById("confirm_new_passwordMsg").innerHTML="";
      if(newPwd!=""&&newPwd!=null)
      {
         if(newPwd!=pwd)
         {
            document.getElementById("confirm_new_passwordMsg").innerHTML="确认新密码与新密码不一致!";
            document.getElementById("confirm_new_password").value="";
            return false;
         }
         else
         {
            document.getElementById("confirm_new_passwordMsg").innerHTML="";
            newPwdPass=true;
            return true;
         }
      }
      else
      {
         document.getElementById("get_new_passwordMsg").innerHTML="新密码不能为空!";
         return false;
      }
     
   }   
}

function validatePwd(pwd)
{
   regularPaw=/^[\dA-Za-z(!@#$%&)]{6,16}$/;
   if(regularPaw.test(pwd)==false)
   {
      return true;
   }
   else
   {
      return false;
   }
}

function updateNewPwd()
{
    if(oldPwdPass&&newPwdPass)
    {
       oldPwdPass=false;
       newPwdPass=false;
       $.ajax({
                 type:"get",
                 url:"UpdatePwd.ashx",//转到一般处理程序
                 data:{"NewPwd":newPwd,"random":Math.random()},              
                 success:function(data)
                 {              
                     if(data=="true")
                     {
                        document.getElementById("get_old_password").value="";
                        document.getElementById("get_new_password").value="";
                        document.getElementById("confirm_new_password").value="";
                        alert("密码修改成功!");
                     }
                     else
                     {
                        document.getElementById("get_old_password").value="";
                        document.getElementById("get_new_password").value="";
                        document.getElementById("confirm_new_password").value="";
                        alert("密码修改失败,请重新修改!");
                     }      
                 }                    
         });
    }
    else
    {
       alert("请填写完整修改密码所必需的信息!");
    }
    return false;
}

</script>
</head>
<body>
    <form id="formBody" runat="server">
    <div>
   
   <div>
   <div>
     <b id="psnB1"></b><h3>修改密码:</h3>
   </div> 
   <div class="ln-thin ln-c-mid"></div>
   <div style="height:1px; background-color:White"></div> 
    <table id="psnB1Table">  
     <tbody>  
       <tr>
         <th><label for="get_cust_name">您的会员登录名是:</label></th>
         <td>
           <asp:Label ID="get_cust_name" runat="server" Text="Label" Font-Bold="True"></asp:Label>                   
         </td>                       
       </tr>                          
       <tr>
         <th><label for="get_old_password">旧密码:</label></th>
         <td>
           <input id="get_old_password" type="password" onblur="checkOldPwd(this.value);" CssClass="required oldpawcl"/>&nbsp;<span style="color:Red; font-size:14px">*</span>
           <label id="get_old_passwordMsg"  class="errorMsg"></label>
         </td>
       </tr>
       <tr>
         <th><label for="get_new_password">新密码:</label></th>
         <td>
           <input id="get_new_password" type="password" onblur="checkNewPwd(this.value);" CssClass="required newpawcl"/>&nbsp;<span style="color:Red; font-size:14px">*</span>
           <label id="get_new_passwordMsg" class="errorMsg"></label>
         </td>
       </tr>
       <tr>
         <th><label for="confirm_new_password">确认新密码:</label></th>
         <td>
           <input id="confirm_new_password" type="password" onblur="checkConfirmNewPwd(this.value);" CssClass="required cfnewpawcl"/>&nbsp;<span style="color:Red; font-size:14px">*</span>
           <label id="confirm_new_passwordMsg" class="errorMsg"></label>
         </td>
       </tr>
         
     </tbody>              
    </table>      
    </div>
    <div style="margin-bottom:-1px">
      <div class="ln-thin ln-c-mid"></div>  
      <div style="height:1px; background-color:White"></div>                
      <div id="psnTable4"></div>               
    </div>
    <table>                   
      <tbody>                       
      <tr>                           
       <th>
           &nbsp;</th>                           
       <td>
         <asp:Button ID="Finish" CssClass="Button" runat="server" Text="修 改" OnClientClick="javascript:return updateNewPwd();" />
       </td>                       
      </tr>                   
      </tbody>
      </table>
   
    </div>
    </form>
</body>
</html>

 


2.后台代码页(Default.aspx.cs):

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["UserId"] = "001";
            Session["UserName"] = "HEHE";
        }
        get_cust_name.Text = Session["UserName"].ToString();//获取当前登录用户的Session中的UserName
    }

}

 


3.一般处理程序1(CheckPwd.ashx):

<%@ WebHandler Language="C#" class="CheckPwd" %>

using System;
using System.Web;

public class CheckPwd : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string pwd = context.Request["Pwd"];

        string userId = context.Session["UserId"].ToString();
        string dbPwd = "123456";//演示密码
               
        //string dbPwd=UpdatePasswordService.GetDbPwd(userId);//自己写的类中的方法,从数据库中取出密码。
        if (pwd == dbPwd)
        {
            context.Response.Write("true");
        }
        else
        {
            context.Response.Write("false");
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

 


4.一般处理程序2(UpdatePwd.ashx):

<%@ WebHandler Language="C#" class="UpdatePwd" %>

using System;
using System.Web;

public class UpdatePwd : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string newPwd = context.Request["NewPwd"];
        string userId = context.Session["UserId"].ToString();
        bool result = true;//演示结果
        //bool result = UpdatePasswordService.UpdatePwd(userId, encryptPwd);//自己写的类中的方法,更新密码并返回是否成功。
        if (result)
        {
            context.Response.Write("true");
        }
        else
        {
            context.Response.Write("false");
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

你可能感兴趣的:(DoNet用Ajax实现对修改密码时输入数据合法性的验证)