- 时间:2021-03-03 05:10 编辑: 来源: 阅读:
- 扫一扫,手机访问
摘要:验证用户必选CheckBox控件与自定义验证javascript代码
CheckBox控件,由于它的值是选择与非选择。因此在提交数据时,想让用户必须选择CheckBox,普通情况之下,不好做验证。
但我们可以使用asp:CustomValidator来验证,不过还得写自定义验证Javascript代码,可参考如下:
function ValidateCheckBox(sender, args) {
var checkbox = document.getElementById("<%=CheckBox1.ClientID %>")
if (checkbox.checked) {
args.IsValid = true;
}
else {
args.IsValid = false;
}
}
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="必须选择选项" ForeColor="Red" ClientValidationFunction="ValidateCheckBox"></asp:CustomValidator><br />
<asp:Button ID="Button1" runat="server" Text="提交" />
[b]演示[/b]:
[img]http://files.jb51.net/file_images/article/201301/2013116100611729.gif?201301610659[/img]