我在asp.net中创建一个简单的注册表单。 这是我的问题: 如果我的确认密码字段为空,它仍然提交表单。我正在使用compareToValidate我还需要使用requiredvalidator。我是asp.net的新手,但如果让密码为123并且确认密码为空,则comparevalidator不会产生错误。
<fieldset>
<legend>Enter your data</legend>
<table>
<tr>
<td align="right">User Name :</td>
<td> </td>
<td align="left">
<asp:TextBox ID="tbUsername" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvUserName" runat="server"
ControlToValidate="tbUsername"
CssClass="ValidationError"
ErrorMessage="« (Required)"
ToolTip="User Name is a REQUIRED field"
></asp:RequiredFieldValidator>
</td>
</tr>`enter code here`
<tr >
<td align="right">Password : </td>
<td> </td>
<td align="left">
<asp:TextBox ID="tbPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="« (Required)"
ControlToValidate="tbPassword"
CssClass="ValidationError"
ToolTip="Password is a REQUIRED field"
></asp:RequiredFieldValidator>
</td>
</tr>
<tr >
<td align="right">Confirm Password : </td>
<td> </td>
<td align="left">
<asp:TextBox ID="ConfirmPass" runat="server" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="ConfirmPass"
CssClass="ValidationError"
ControlToCompare="tbPassword"
ErrorMessage="No Match"
ToolTip="Password must be the same" />
</td>
</tr>
答案 0 :(得分:21)
您还必须提供必要的验证器!比较验证器只会将值与目标控件进行比较,并提醒用户。
<td align="left">
<asp:TextBox ID="ConfirmPass" runat="server" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="ConfirmPass"
CssClass="ValidationError"
ControlToCompare="tbPassword"
ErrorMessage="No Match"
ToolTip="Password must be the same" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="« (Required)"
ControlToValidate="ConfirmPass"
CssClass="ValidationError"
ToolTip="Compare Password is a REQUIRED field">
</asp:RequiredFieldValidator>
</td>
希望这能帮到你!
答案 1 :(得分:1)
恕我直言,这应该是什么。 CompareValidator控件用于将一个输入控件的值与另一个输入控件的值或固定值进行比较。
如果输入控件为空,则不会调用验证函数并且验证成功。使用RequiredFieldValidator控件来阻止用户跳过输入控件。
参考:http://msdn.microsoft.com/en-us/library/db330ayw(v=vs.100).aspx
答案 2 :(得分:0)
简单方法
它真的很有效
<asp:CompareValidator runat="server" ID="Comp1" ControlToValidate="tbPassword" ControlToCompare="ConfirmPass" Text="Password mismatch" Font-Size="11px" ForeColor="Red" />