Skip to content

<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

html
<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" />&nbsp;<CancelButton Text="Cancel" />
        <ValidationSummary />
      </td>
    </tr>
  </table>
</AddForm>

Properties

PropertyValuesDefaultDescription
Type *CheckboxIdentifies this as a Checkbox validator
Target *control IDID of the <Checkbox> to validate
MustBeCheckedTrue FalseTrueThe state the checkbox must be in to pass validation
CssClassstringCSS class name(s) for styling the validator's error display
DisplayStatic DynamicDynamicWhether the validator reserves layout space when no error is shown
EnableClientScriptTrue FalseTrueWhen True, validation runs in the browser as well as on the server
HeightsizeHeight of the validator's error display
MessagestringText shown in the <ValidationSummary> when validation fails
TextstringText shown inline at the validator's location when validation fails
WidthsizeWidth 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.

PropertyValuesDescription
BackColorcolor name | #ddddddBackground color of the validator's error display
BorderColorcolor name | #ddddddBorder color
BorderStyleNotSet None Dotted Dashed Solid Double Groove Ridge Inset OutsetBorder style
BorderWidthsizeBorder width
Font-BoldTrue FalseBold text
Font-ItalicTrue FalseItalic text
Font-NamesstringFont family name
Font-OverlineTrue FalseOverline text decoration
Font-SizeXX-Small X-Small Small Medium Large X-Large XX-Large or sizeFont size
Font-StrikeoutTrue FalseStrikethrough text decoration
Font-UnderlineTrue FalseUnderline text decoration
ForeColorcolor name | #ddddddText color of the error display

Property Details

  • Type: Set to Checkbox to identify this as a Checkbox validator.

  • Target: The ID of 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; False means 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 Message and <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. Static always 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 mirrors MustBeChecked, so the user gets immediate feedback without a postback.