我在asp.net中创建一个简单的注册表单.
这是我的问题:
如果我的确认密码字段为空,它仍然提交表单.我正在使用compareToValidate我还需要使用requiredvalidator.我是asp.net的新手,但如果让密码为123并且确认密码为空,则comparevalidator不会产生错误.
这是我的问题:
如果我的确认密码字段为空,它仍然提交表单.我正在使用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>
解决方法
您还必须提供必要的验证器!比较验证器仅将值与目标控件进行比较并提醒用户.
- <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>
希望这对你有所帮助!