Angular Validator - CompareRule

A validation rule that requires validated values to match a specified expression.

import { CompareRule } from "devextreme/common"
Type:

Object

To specify a comparison expression, define the comparisonTarget function. Validator compares values to this function's return value. To configure which comparison operator Validator compares values to, specify the comparisonType property.

app.component.html
app.component.ts
app.module.ts
  • <dx-text-box
  • [(value)]="password"
  • ></dx-text-box>
  • <dx-text-box>
  • <dx-validator>
  • <dxi-validator-validation-rule
  • type="compare"
  • [comparisonTarget]="passwordComparison"
  • message="Passwords do not match."
  • ></dxi-validator-validation-rule>
  • </dx-validator>
  • </dx-text-box>
  • export class AppComponent {
  • password = '';
  • passwordComparison = () => this.password;
  • }
  • import { BrowserModule } from '@angular/platform-browser';
  • import { NgModule } from '@angular/core';
  • import { AppComponent } from './app.component';
  •  
  • import { DxValidatorModule, DxTextBoxModule } from 'devextreme-angular';
  •  
  • @NgModule({
  • declarations: [
  • AppComponent
  • ],
  • imports: [
  • DxTextBoxModule,
  • BrowserModule,
  • DxValidatorModule
  • ],
  • providers: [],
  • bootstrap: [AppComponent]
  • })
  • export class AppModule { }

View Demo

See Also

comparisonTarget

Specifies the function whose return value is used for comparison with the validated value.

Type:

Function

Return Value: any

A value to be compared with the validated value.

The rule is valid if the comparison between the validated value and the return value of the comparisonTarget function evaluates to true. The operator specified by the comparisonType property is used to compare values.

comparisonType

Specifies the operator to be used for comparing the validated value with the target.

Default Value: '=='

ignoreEmptyValue

If set to true, empty values are valid.

Type:

Boolean

Default Value: false

message

Specifies the message that is shown if the rule is broken.

Type:

String

Default Value: 'Values do not match'

You can configure an error message in the following ways:

  • Specify a hard-coded message:

    app.component.html
    app.module.ts
    • <dx-text-box>
    • <dx-validator>
    • <dxi-validator-validation-rule
    • type="compare"
    • message="My custom message">
    • </dxi-validator-validation-rule>
    • </dx-validator>
    • </dx-text-box>
    • import { DxValidatorModule, DxTextBoxModule } from 'devextreme-angular';
    • // ...
    •  
    • @NgModule({
    • imports: [
    • DxTextBoxModule,
    • DxValidatorModule,
    • ],
    • })
  • Hide the message:

    app.component.html
    • <dx-text-box>
    • <dx-validator>
    • <dxi-validator-validation-rule
    • type="compare"
    • message="">
    • </dxi-validator-validation-rule>
    • </dx-validator>
    • </dx-text-box>
  • Display the editor's name in the default message (for instance, "Passwords do not match"):

    app.component.html
    • <dx-text-box>
    • <dx-validator name="Passwords">
    • <dxi-validator-validation-rule
    • type="compare"
    • ></dxi-validator-validation-rule>
    • </dx-validator>
    • </dx-text-box>

type

Specifies the rule type. Set it to "compare" to use the CompareRule.