The CheckBox control, because its value is selected and not selected. Therefore, when submitting the data, want to let the user must choose CheckBox, under normal circumstances, not to do validation.
But we can use Asp:customvalidator to verify, but we have to write custom validation JavaScript code, can refer to the following:
Copy Code code as follows:
function Validatecheckbox (sender, args) {
var checkbox = document.getElementById ("<%=checkbox1.clientid%>")
if (checkbox.checked) {
Args. IsValid = true;
}
else {
Args. IsValid = false;
}
}
Copy Code code as follows:
<asp:checkbox id= "CheckBox1" runat= "Server"/>
<asp:customvalidator id= "CustomValidator1" runat= "server" errormessage= "must select Options" forecolor= "Red" clientvalidationfunction= "Validatecheckbox" ></asp:customvalidator><br/>
<asp:button id= "Button1" runat= "Server" text= "Submit"/>
Demo: