Skip to content

<Validate Type="Compare">

The Compare validator checks the target control's value against another control's value or a hard-coded value, using a configurable comparison operator. The classic use case is "make sure the password and confirm-password fields match," but it works equally well for date ranges, type checks, and minimum-value comparisons.

Example

Confirming an email address by comparing two textboxes:

html
<AddForm>
  ...
  <table>
    <tr>
      <td>
        <Label For="txtEmail" Text="Email" />
        <TextBox Id="txtEmail" DataField="Email" DataType="string" />
      </td>
    </tr>
    <tr>
      <td>
        <Label For="txtEmailConfirm" Text="Confirm Email" />
        <TextBox Id="txtEmailConfirm" />
        <Validate Type="Compare" Target="txtEmailConfirm" CompareTarget="txtEmail"
                  Message="The email addresses don't match" />
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <AddButton Text="Add"/>&nbsp;<CancelButton Text="Cancel"/>
        <ValidationSummary />
      </td>
    </tr>
  </table>
</AddForm>

Properties

PropertyValuesDefaultDescription
Type *CompareIdentifies this as a Compare validator
Target *control IDID of the control to validate
CompareTargetcontrol IDID of the control whose value to compare against. Use either CompareTarget or CompareValue, not both
CompareValuestringHard-coded value to compare against. Use either CompareTarget or CompareValue, not both
CssClassstringCSS class name(s) for styling the validator's error display
DataTypeString Integer Double Date CurrencyStringThe data type used for the comparison
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
OperatorEqual NotEqual GreaterThan GreaterThanEqual LessThan LessThanEqual DataTypeCheckEqualThe comparison operator
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. Note: the validator hard-codes red, bold text by default. If you set CssClass, XMP automatically clears ForeColor so your stylesheet's color rules take effect

Property Details

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

  • Target: The ID of the form control whose value should be checked.

  • CompareTarget: The ID of a second control whose value the target should be compared with. Use this or CompareValue, not both. Maps to ASP.NET's ControlToCompare.

  • CompareValue: A hard-coded value the target should be compared with (e.g. CompareValue="18" to require a minimum age). Use this or CompareTarget, not both. Maps to ASP.NET's ValueToCompare.

  • Operator: How the two values are compared. Defaults to Equal. The special value DataTypeCheck ignores any compare value and instead just verifies that the target control's value can be parsed as the specified DataType — useful as a quick "is this a valid number/date?" check without needing a regex.

  • DataType: How the values should be parsed before comparison. Defaults to String (literal text comparison). Set to Integer, Double, Date, or Currency for type-aware comparisons.

  • 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 before the form is submitted, giving the user immediate feedback. Set to False to force server-side-only validation.