CheckBox控件,由于它的值是选择与非选择。因此在提交数据时,想让用户必须选择CheckBox,普通情况之下,不好做验证。

但我们可以使用asp:CustomValidator来验证,不过还得写自定义验证Javascript代码,可参考如下:

View Code
 1  function ValidateCheckBox(sender, args) {
 2             var checkbox = document.getElementById("<%=CheckBox1.ClientID %>")
 3 
 4             if (checkbox.checked) {
 5                 args.IsValid = true;
 6             }
 7             else {
 8                 args.IsValid = false;
 9             }
10         }

 

View Code
1 <asp:CheckBox ID="CheckBox1" runat="server" />
2             <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="必须选择选项" ForeColor="Red"  ClientValidationFunction="ValidateCheckBox"></asp:CustomValidator><br />
3             <asp:Button ID="Button1" runat="server" Text="提交" />


演示:

验证用户必选CheckBox控件

相关文章:

  • 2021-09-26
  • 2021-08-01
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
  • 2021-08-22
猜你喜欢
  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案