ASP.Net - Custom Validator for a Checkbox
There seems to be a lot of people having issues with client-side validation of a checkbox – so one of my colleagues wrote a new custom validator script that works well.
<head runat="server">
<script type="text/javascript">
function valCheckBox(source, args) {
var chk = document.getElementById("chkTest")
if (chk.checked) {
args.IsValid = true;
return;
}
else {
args.IsValid = false;
return;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBox ID="chkTest" runat="server" />
<asp:CustomValidator ID="valTest" runat="server" ClientValidationFunction="valCheckBox" Text="*" />
<asp:Button ID="cmdTest" runat="server" Text="Validate" />
</form>
</body>