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-validation-rule
  • type="compare"
  • [comparisonTarget]="passwordComparison"
  • message="Passwords do not match."
  • ></dxi-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'

An error message can be specified as follows:

  • Hard-code the message

    app.component.html
    app.module.ts
    • <dx-text-box>
    • <dx-validator>
    • <dxi-validation-rule
    • type="compare"
    • message="My custom message">
    • </dxi-validation-rule>
    • </dx-validator>
    • </dx-text-box>
    • 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 { }
  • Hide the message

    app.component.html
    app.module.ts
    • <dx-text-box>
    • <dx-validator>
    • <dxi-validation-rule
    • type="compare"
    • message="">
    • </dxi-validation-rule>
    • </dx-validator>
    • </dx-text-box>
    • 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: [
    • BrowserModule,
    • DxValidatorModule,
    • DxTextBoxModule
    • ],
    • providers: [],
    • bootstrap: [AppComponent]
    • })
    • export class AppModule { }
  • Display the editor's name in the message

    app.component.html
    app.module.ts
    • <dx-text-box>
    • <!-- The error message will be "Passwords do not match" -->
    • <dx-validator name="Passwords">
    • <dxi-validation-rule
    • type="compare">
    • </dxi-validation-rule>
    • </dx-validator>
    • </dx-text-box>
    • 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: [
    • BrowserModule,
    • DxValidatorModule,
    • DxTextBoxModule
    • ],
    • providers: [],
    • bootstrap: [AppComponent]
    • })
    • export class AppModule { }

type

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