2011-11-17 86 views
1

我正在使用更改密码向导来更改密码。是否有任何方法检查输入的密码和新密码是否相同,如果相同,则显示错误消息?我尝试使用代码,但它给出了错误消息,并且还提供了一条成功消息,指出密码已更改。是否可以把一些比较验证器来检查这些值?更改密码控制验证

+0

随着张贴出你所编写的代码,我们怎么能知道什么在你的代码中的错误? –

+0

标准更改密码控制具有验证新密码和确认新密码之间的比较。但不在当前密码和新密码之间 – user662417

+0

您可以将转换为模板后的invalid-logincontrol中的比较验证程序 它将工作 –

回答

0

我得到了解决。代替使用更改密码模板,我使用代码

protected void ChangePassword1_ChangedPassword(object sender, EventArgs e) 
    { 

     if (ChangePassword1.CurrentPassword == ChangePassword1.NewPassword) 
     { 
      Response.Redirect("ChangePassword.aspx"); 

     } 
     //Label1.Text = "current and new passwords should not match";   
     Label1.Visible = false; 
    } 

最初lable1输入了不同的当前密码和新密码。

2

在您的更改密码网页上添加一个验证器。 尝试以下方法验证:

<asp:CompareValidator ID="CompareValidator1" 
    runat="server" 
    ControlToCompare="NewPassword" // ID of your new password field 
    ControlToValidate="CurrentPassword" //ID of current password field 
    ErrorMessage="You should enter different password." 
    ForeColor="Red"> 
</asp:CompareValidator> 
+0

我尝试使用ur代码。它出现错误,如“无法找到'CompareValidator1'的'ControlToCompare'属性引用的控件ID'NewPassword'”。 – user662417

+0

@ user662417:你不应该像NewPassword一样使用,你应该使用你拥有的域名的id –

+1

你应该用你的控件ID替换“New Password”。这只是一个例子。 –

0

如果你创建自己的控制,

1. check the current user who is logged in 
2. when user try to change the password, probably a button click 
    get the users unique id and match the new password and old password. 
3. If password match return an error, update the database with new password otherwise 
+0

我没有创建自己的控件。我正在使用changepassword向导 – user662417

0

尝试

ASP:CompareValidator

与运营商 “属性= NotEqual”

这应该工作

0

你应该有两个文本框,让我们说txtNewPasswordtxtRePass。这将完成这项工作,如下图所示,<asp:CompareValidator>将完成这项工作。 但是,如果你也想要一个额外的验证,你还可以添加Validation Group = ""

<asp:CompareValidator runat="server" ID="Comp1" ControlToValidate="txtNewPassword" ControlToCompare="txtRePass" Text="Password mismatch" Font-Size="11px" ForeColor="Red"/>`