The CheckBox control, because its value is select and non-select. Therefore, when submitting data, users must select CheckBox. In normal cases, verification is not recommended.
However, we can use asp: mmvalidator for verification, but we have to write custom Javascript code for verification. For details, refer to the following:
Copy codeThe Code is as follows:
Function ValidateCheckBox (sender, args ){
Var checkbox = document. getElementById ("<% = CheckBox1.ClientID %> ")
If (checkbox. checked ){
Args. IsValid = true;
}
Else {
Args. IsValid = false;
}
}
Copy codeThe Code is as follows:
<Asp: CheckBox ID = "CheckBox1" runat = "server"/>
<Asp: CustomValidator ID = "CustomValidator1" runat = "server" ErrorMessage = "required" ForeColor = "Red" ClientValidationFunction = "ValidateCheckBox"> </asp: customValidator> <br/>
<Asp: Button ID = "Button1" runat = "server" Text = "Submit"/>
Demo: