我在页面中有3个文本框。其中一个文本框从用户处取得新的密码,根据用户输入的密码,标签显示密码强度消息。但是在显示消息之后通过标签,textBox文本被清除。有没有办法保留文本?我已经启用了文本框的自动回送,因为我需要为它使用Comparevalidator。 下面是代码snippet-在回发事件后保留文本框文本
protected void NewPassEntered(object sender, EventArgs e)
{
if (txtPassword.Text.Length < 4)
{
lblPassStr.Visible = Visible;
lblPassStr.BackColor = System.Drawing.Color.OrangeRed;
lblPassStr.Text = "Password should have more than four characters";
txtPassword.Text = "";
}
else if ((txtPassword.Text.Length > 4) && (txtPassword.Text.Length < 6) && (txtPassword.Text.Contains("@")))
{
lblPassStr.Visible = Visible;
lblPassStr.BackColor = System.Drawing.Color.Green;
lblPassStr.Text = "Password Strength:Medium";
}
else if ((txtPassword.Text.Length > 4) && (txtPassword.Text.Length < 6))
{
lblPassStr.Visible = Visible;
lblPassStr.BackColor = System.Drawing.Color.Yellow;
lblPassStr.Text = "Password Strength:Weak";
}
else if ((txtPassword.Text.Length > 6) && (txtPassword.Text.Contains("@")))
{
lblPassStr.Visible = Visible;
lblPassStr.BackColor = System.Drawing.Color.Blue;
lblPassStr.Text = "Password Strength:Strong";
}
}
}
有没有一种方法能够有效地检查多个特殊字符?
的ASPX代码看起来像这个 - :
<div style="width:400px; height:250px;border-color:GoldenRod ;border-style:solid;border-width:thin;padding:20px 50px 50px 20px; position:relative; margin:100px 100px; margin-left:344px">
<table border="0" align="center" cellpadding="0" cellspacing="0" width="350" >
<tr><td> <br /></td></tr>
<tr>
<td style="width:200px">
<span class="labeltxt"> Old Password: </span>
<br /></td></tr><tr> <td style="width:200px"> <asp:TextBox ID="txtoldpass"
runat="server" CssClass="text" TextMode="Password" Width="300px"
Height="25" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*Enter the old password" ControlToValidate="txtoldpass" Text="*" ForeColor="Red" InitialValue="">
</asp:RequiredFieldValidator><br />
</td>
</tr>
<tr>
<td><br /></td>
</tr>
<tr>
<td style="width:200px">
<span>New Password: </span>
<br /></td></tr><tr>
<td style="width:200px">
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" CssClass="text"
Width="300px" Height="25"
ontextchanged="NewPassEntered"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Font-Italic="true" ErrorMessage="**Enter the new password!" ControlToValidate="txtPassword" Text="**" ForeColor="Red" InitialValue="">
</asp:RequiredFieldValidator>
<br /></td>
</tr>
<tr>
<td><br />
<asp:Label ID="lblPassStr" runat="server" Text="Label" Visible="False"></asp:Label></td>
</tr>
<tr>
<td style="width:200px">
<span>Confirm Password:</span>
<br /> </td></tr>
<tr>
<td style="width:200px"> <asp:TextBox ID="txtPassword1" runat="server"
TextMode="Password" CssClass="text" Width="300px" Height="25"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="***Enter the new password again!" ControlToValidate="txtPassword1" Text="***" ForeColor="Red" InitialValue="">
</asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="Passwords do not Match!" ControlToCompare="txtPassword"
ControlToValidate="txtPassword1"></asp:CompareValidator>
<br /> </td>
</tr>
<tr>
<td><br /><br /></td>
</tr>
<tr>
<td>
<asp:Button runat="server" ID="btnLogin" Text="Save" Height="25px"
Width="76px" CssClass="btn" BackColor="Goldenrod" onclick="btnLogin_Click"></asp:Button><asp:HyperLink ID="HyperLink1" runat="server">Cancel</asp:HyperLink>
</td>
</tr>
</table>
</div>
<asp:ValidationSummary ID="ValidationSummary1" Font-Italic="true" font-size="Small" forecolor="Black" runat="server" />
为什么不使用客户端JavaScript来检查/验证密码强度? – StevieB