<Validate Type="Checkbox">
The Checkbox validator prevents the form from being submitted unless the target <Checkbox> is in the required state. The classic case is a "I agree to the terms" checkbox that the user must tick — but the validator can also require a checkbox to be unchecked via MustBeChecked="False".
Why not Validate Type="Required"?
A checkbox always has a value (it's either checked or unchecked), so a "required" check would always pass. The Checkbox validator exists specifically to express "this checkbox must be in this state."
Example
<AddForm>
<SubmitCommand CommandText="INSERT INTO Users(FirstName, LastName, Agree)
VALUES(@FirstName, @LastName, @Agree)" />
<table>
<tr>
<td>
<Label For="txtFirstName" Text="First Name" />
<TextBox Id="txtFirstName" DataField="FirstName" DataType="string" />
</td>
</tr>
<tr>
<td>
<Label For="chkAgree" Text="I agree to the Terms of Service" />
<Checkbox Id="chkAgree" DataField="Agree" DataType="boolean" />
<Validate Type="Checkbox" Target="chkAgree" MustBeChecked="True"
Message="You must agree to the terms" />
</td>
</tr>
<tr>
<td colspan="2">
<AddButton Text="Add" /> <CancelButton Text="Cancel" />
<ValidationSummary />
</td>
</tr>
</table>
</AddForm>Properties
| Property | Values | Default | Description |
|---|---|---|---|
| Type * | Checkbox | Identifies this as a Checkbox validator | |
| Target * | control ID | ID of the <Checkbox> to validate | |
| MustBeChecked | True False | True | The state the checkbox must be in to pass validation |
| CssClass | string | CSS class name(s) for styling the validator's error display | |
| Display | Static Dynamic | Dynamic | Whether the validator reserves layout space when no error is shown |
| EnableClientScript | True False | True | When True, validation runs in the browser as well as on the server |
| Height | size | Height of the validator's error display | |
| Message | string | Text shown in the <ValidationSummary> when validation fails | |
| Text | string | Text shown inline at the validator's location when validation fails | |
| Width | size | Width of the validator's error display |
* Required property
Deprecated Properties
These properties use ASP.NET inline styling and are no longer recommended for modern web development. Use the CssClass property to apply CSS classes or the Style property for inline CSS instead.
| Property | Values | Description |
|---|---|---|
| BackColor | color name | #dddddd | Background color of the validator's error display |
| BorderColor | color name | #dddddd | Border color |
| BorderStyle | NotSet None Dotted Dashed Solid Double Groove Ridge Inset Outset | Border style |
| BorderWidth | size | Border width |
| Font-Bold | True False | Bold text |
| Font-Italic | True False | Italic text |
| Font-Names | string | Font family name |
| Font-Overline | True False | Overline text decoration |
| Font-Size | XX-Small X-Small Small Medium Large X-Large XX-Large or size | Font size |
| Font-Strikeout | True False | Strikethrough text decoration |
| Font-Underline | True False | Underline text decoration |
| ForeColor | color name | #dddddd | Text color of the error display |
Property Details
Type: Set to
Checkboxto identify this as a Checkbox validator.Target: The
IDof the<Checkbox>control whose state should be checked. The control must be a<Checkbox>— the validator throws an exception at runtime if pointed at any other control type.MustBeChecked: The state the checkbox must be in to pass validation.
True(the default) means the box must be checked;Falsemeans it must be unchecked.Message: The error text shown in the
<ValidationSummary>(if you have one) when validation fails. If no<ValidationSummary>is present, this text appears at the validator's location instead.Text: The text shown inline at the validator's location when validation fails. Used together with
Messageand<ValidationSummary>: a short inline marker (*,**, or an icon) at the validator + the full sentence in the summary block.Display: Whether the validator reserves layout space even when no error is shown.
Dynamic(the default) collapses to no space until validation fails.Staticalways reserves space.EnableClientScript: When
True(the default), the validator runs in the browser as well as on the server. The check is implemented as a small JavaScript expando attribute that mirrorsMustBeChecked, so the user gets immediate feedback without a postback.