asp.net – ASP:NET确认密码字段

前端之家收集整理的这篇文章主要介绍了asp.net – ASP:NET确认密码字段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在asp.net中创建一个简单的注册表单.
这是我的问题:
如果我的确认密码字段为空,它仍然提交表单.我正在使用compareToValidate我还需要使用requiredvalidator.我是asp.net的新手,但如果让密码为123并且确认密码为空,则comparevalidator不会产生错误.
  1. <fieldset>
  2. <legend>Enter your data</legend>
  3. <table>
  4. <tr>
  5. <td align="right">User Name :</td>
  6. <td>&nbsp;</td>
  7. <td align="left">
  8. <asp:TextBox ID="tbUsername" runat="server"></asp:TextBox>
  9. <asp:requiredFieldValidator ID="rfvUserName" runat="server"
  10. ControlToValidate="tbUsername"
  11. CssClass="ValidationError"
  12. ErrorMessage="&laquo; (required)"
  13. ToolTip="User Name is a required field"
  14. ></asp:requiredFieldValidator>
  15. </td>
  16. </tr>`enter code here`
  17. <tr >
  18. <td align="right">Password&nbsp;:&nbsp;</td>
  19. <td>&nbsp;</td>
  20. <td align="left">
  21. <asp:TextBox ID="tbPassword" runat="server" TextMode="Password"></asp:TextBox>
  22. <asp:requiredFieldValidator ID="requiredFieldValidator1" runat="server"
  23. ErrorMessage="&laquo; (required)"
  24. ControlToValidate="tbPassword"
  25. CssClass="ValidationError"
  26.  
  27. ToolTip="Password is a required field"
  28. ></asp:requiredFieldValidator>
  29. </td>
  30.  
  31. </tr>
  32. <tr >
  33. <td align="right">Confirm Password&nbsp;:&nbsp;</td>
  34. <td>&nbsp;</td>
  35. <td align="left">
  36. <asp:TextBox ID="ConfirmPass" runat="server" TextMode="Password"></asp:TextBox>
  37. <asp:CompareValidator ID="CompareValidator1" runat="server"
  38. ControlToValidate="ConfirmPass"
  39. CssClass="ValidationError"
  40. ControlToCompare="tbPassword"
  41. ErrorMessage="No Match"
  42. ToolTip="Password must be the same" />
  43.  
  44. </td>
  45.  
  46. </tr>

解决方法

您还必须提供必要的验证器!比较验证器仅将值与目标控件进行比较并提醒用户.
  1. <td align="left">
  2. <asp:TextBox ID="ConfirmPass" runat="server" TextMode="Password"></asp:TextBox>
  3.  
  4. <asp:CompareValidator ID="CompareValidator1" runat="server"
  5. ControlToValidate="ConfirmPass"
  6. CssClass="ValidationError"
  7. ControlToCompare="tbPassword"
  8. ErrorMessage="No Match"
  9. ToolTip="Password must be the same" />
  10.  
  11. <asp:requiredFieldValidator ID="requiredFieldValidator2" runat="server"
  12. ErrorMessage="&laquo; (required)"
  13. ControlToValidate="ConfirmPass"
  14. CssClass="ValidationError"
  15. ToolTip="Compare Password is a required field">
  16. </asp:requiredFieldValidator>
  17. </td>

希望这对你有所帮助!

猜你在找的asp.Net相关文章