8

我在 asp.net 中创建了一个简单的注册表单。这是我的问题:如果我的确认密码字段为空,它仍会提交表单。我正在使用 compareToValidate 是否还需要使用 requiredvalidator。我是 asp.net 的新手,但如果假设密码为 123 并且确认密码为空,则 comparevalidator 不应该生成错误。

        <fieldset>
       <legend>Enter your data</legend>
       <table>
        <tr>
       <td align="right">User Name :</td>
       <td>&nbsp;</td>
         <td align="left">
        <asp:TextBox ID="tbUsername" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="rfvUserName" runat="server" 
        ControlToValidate="tbUsername" 
        CssClass="ValidationError"
        ErrorMessage="&laquo; (Required)"
        ToolTip="User Name is a REQUIRED field"
        ></asp:RequiredFieldValidator>
        </td>
</tr>`enter code here`
<tr >
<td align="right">Password&nbsp;:&nbsp;</td>
<td>&nbsp;</td>
<td align="left"> 
    <asp:TextBox ID="tbPassword" runat="server" TextMode="Password"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
     ErrorMessage="&laquo; (Required)" 
     ControlToValidate="tbPassword"
     CssClass="ValidationError"

    ToolTip="Password is a REQUIRED field"
    ></asp:RequiredFieldValidator>
</td>

</tr>
<tr >
<td align="right">Confirm Password&nbsp;:&nbsp;</td>
<td>&nbsp;</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>

4

3 回答 3

22

您还必须提供必需的验证器!比较验证器只会将值与目标控件进行比较并提醒用户。

<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="&laquo; (Required)" 
     ControlToValidate="ConfirmPass"
     CssClass="ValidationError"
     ToolTip="Compare Password is a REQUIRED field">
    </asp:RequiredFieldValidator>
</td>

希望这对你有帮助!

于 2013-05-30T13:16:28.040 回答
1

恕我直言,这正是它应该做的。CompareValidator 控件用于将一个输入控件的值与另一个输入控件的值或与一个固定值进行比较。

如果输入控件为空,则不调用验证函数并且验证成功。使用RequiredFieldValidator 控件来防止用户跳过输入控件。

参考: http: //msdn.microsoft.com/en-us/library/db330ayw (v=vs.100).aspx

于 2013-05-30T13:18:49.933 回答
1

简单的方法 它真的有效 <asp:CompareValidator runat="server" ID="Comp1" ControlToValidate="tbPassword" ControlToCompare="ConfirmPass" Text="Password mismatch" Font-Size="11px" ForeColor="Red" />

于 2016-06-17T11:47:35.337 回答