DevExtreme v23.2 is now available.

Explore our newest features/capabilities and share your thoughts with us.

Your search did not match any results.

Validation

This demo shows how to implement a default validation group - a group of editors on a page with enabled data validation. In this particular demo, the editors are grouped in an HTML form.

To enable data validation for an editor, you need to declare the Validator component and implement validation rules. You can attach multiple validation rules to one component. The following list contains all available validation rule types:

  • required
    A validation rule that requires the validated field to have a value.

  • email
    A validation rule that requires the validated field to match the Email pattern.

  • async
    A custom validation rule used for server-side validation. Implement the validationCallback function to validate the target value.

  • compare
    A validation rule that requires the validated editor's value to equal the value of the specified expression. To apply this rule, implement the comparisonTarget function to specify the value against which this component compares the validated value.

  • pattern
    A validation rule that requires the validated field to match a specified pattern.

  • stringLength
    A validation rule that requires the target value length to fall within the range of the specified minimum and maximum values. This property only accepts string values.

  • range
    A validation rule that requires the target value length to fall within the range of the specified minimum and maximum values. This property only accepts date-time and numeric values.

  • numeric
    A validation rule that requires the validated field to have a numeric value.

  • custom
    A rule with custom validation logic. Implement the validationCallback function to validate the target value.

All validation rule types allow you to specify an error message for a component. You can also specify the message position for each editor. If your application implements a validation group, you can use a validation summary to display all validation errors in one place. In this demo, click the Register button to see a validation summary.

The Register button's useSubmitBehavior property is enabled. As a result, a click on the button validates and submits the HTML form. In your application, you can also implement the button's onClick event handler and use the validate() or validateGroup() method to validate a group of editors.

Backend API
<form action="your-action" (submit)="onFormSubmit($event)"> <div class="dx-fieldset"> <div class="dx-fieldset-header">Credentials</div> <div class="dx-field"> <div class="dx-field-label">Email</div> <div class="dx-field-value"> <dx-text-box [inputAttr]="{ 'aria-label': 'Email' }"> <dx-validator> <dxi-validation-rule type="required" message="Email is required" ></dxi-validation-rule> <dxi-validation-rule type="email" message="Email is invalid" ></dxi-validation-rule> <dxi-validation-rule type="async" message="Email is already registered" [validationCallback]="asyncValidation" ></dxi-validation-rule> </dx-validator> </dx-text-box> </div> </div> <div class="dx-field"> <div class="dx-field-label">Password</div> <div class="dx-field-value"> <dx-text-box [inputAttr]="{ 'aria-label': 'Password' }" [mode]="passwordMode" [(value)]="password" (onValueChanged)="onPasswordChanged()" > <dxi-button name="password" location="after" [options]="passwordButton" ></dxi-button> <dx-validator> <dxi-validation-rule type="required" message="Password is required" ></dxi-validation-rule> </dx-validator> </dx-text-box> </div> </div> <div class="dx-field"> <div class="dx-field-label">Confirm Password</div> <div class="dx-field-value"> <dx-text-box [(value)]="confirmPassword" [mode]="confirmPasswordMode" [inputAttr]="{ 'aria-label': 'Password' }" > <dxi-button name="password" location="after" [options]="confirmPasswordButton" ></dxi-button> <dx-validator #targetValidator> <dxi-validation-rule type="required" message="Confirm Password is required" ></dxi-validation-rule> <dxi-validation-rule type="compare" [comparisonTarget]="passwordComparison" message="Password and Confirm Password do not match" ></dxi-validation-rule> </dx-validator> </dx-text-box> </div> </div> </div> <div class="dx-fieldset"> <div class="dx-fieldset-header">Personal Data</div> <div class="dx-field"> <div class="dx-field-label">Name</div> <div class="dx-field-value"> <dx-text-box value="Peter" [inputAttr]="{ 'aria-label': 'Name' }"> <dx-validator> <dxi-validation-rule type="required" message="Name is required" ></dxi-validation-rule> <dxi-validation-rule type="pattern" [pattern]="namePattern" message="Do not use digits in the Name" ></dxi-validation-rule> <dxi-validation-rule type="stringLength" [min]="2" message="Name must have at least 2 symbols" ></dxi-validation-rule> </dx-validator> </dx-text-box> </div> </div> <div class="dx-field"> <div class="dx-field-label">Date of birth</div> <div class="dx-field-value"> <dx-date-box invalidDateMessage="The date must have the following format: MM/dd/yyyy" [inputAttr]="{ 'aria-label': 'Date Of Birth' }" > <dx-validator> <dxi-validation-rule type="required" message="Date of birth is required" ></dxi-validation-rule> <dxi-validation-rule type="range" [max]="maxDate" message="You must be at least 21 years old" ></dxi-validation-rule> </dx-validator> </dx-date-box> </div> </div> <div class="dx-field"> <div class="dx-field-label">Vacation Dates</div> <div class="dx-field-value"> <dx-date-range-box> <dx-validator> <dxi-validation-rule type="custom" message="The vacation period must not exceed 25 days" [validationCallback]="validateVacationDatesRange" > </dxi-validation-rule> <dxi-validation-rule type="custom" message="Both start and end dates must be selected" [validationCallback]="validateVacationDatesPresence" > </dxi-validation-rule> </dx-validator> </dx-date-range-box> </div> </div> </div> <div class="dx-fieldset"> <div class="dx-fieldset-header">Billing address</div> <div class="dx-field"> <div class="dx-field-label">Country</div> <div class="dx-field-value"> <dx-select-box [dataSource]="countries" [inputAttr]="{ 'aria-label': 'Country' }" validationMessagePosition="left" > <dx-validator> <dxi-validation-rule type="required" message="Country is required" ></dxi-validation-rule> </dx-validator> </dx-select-box> </div> </div> <div class="dx-field"> <div class="dx-field-label">City</div> <div class="dx-field-value"> <dx-text-box validationMessagePosition="left" [inputAttr]="{ 'aria-label': 'City' }" > <dx-validator> <dxi-validation-rule type="required" message="City is required" ></dxi-validation-rule> <dxi-validation-rule type="pattern" [pattern]="cityPattern" message="Do not use digits in the City name" ></dxi-validation-rule> <dxi-validation-rule type="stringLength" [min]="2" message="City must have at least 2 symbols" ></dxi-validation-rule> </dx-validator> </dx-text-box> </div> </div> <div class="dx-field"> <div class="dx-field-label">Address</div> <div class="dx-field-value"> <dx-text-box validationMessagePosition="left" [inputAttr]="{ 'aria-label': 'Address' }" > <dx-validator> <dxi-validation-rule type="required" message="Address is required" ></dxi-validation-rule> </dx-validator> </dx-text-box> </div> </div> <div class="dx-field"> <div class="dx-field-label">Phone <i>(optional)</i></div> <div class="dx-field-value"> <dx-text-box [inputAttr]="{ 'aria-label': 'Phone' }" mask="+1 (X00) 000-0000" [maskRules]="phoneRules" maskInvalidMessage="The phone must have a correct USA phone format" validationMessagePosition="left" > <dx-validator> <dxi-validation-rule type="pattern" [pattern]="phonePattern" message="The phone must have a correct USA phone format" ></dxi-validation-rule> </dx-validator> </dx-text-box> </div> </div> </div> <div class="dx-fieldset"> <div class="dx-field"> <div class="dx-field-label"> <dx-check-box id="check" [value]="false" text="I agree to the Terms and Conditions" validationMessagePosition="right" > <dx-validator> <dxi-validation-rule type="compare" [comparisonTarget]="checkComparison" message="You must agree to the Terms and Conditions" ></dxi-validation-rule> </dx-validator> </dx-check-box> </div> <div class="dx-field-value"> <dx-button width="120px" id="button" text="Register" type="default" [useSubmitBehavior]="true" > </dx-button> <dx-validation-summary id="summary"></dx-validation-summary> </div> </div> </div> </form>
import { Component, NgModule, enableProdMode, ViewChild, } from '@angular/core'; import { BrowserModule, BrowserTransferStateModule } from '@angular/platform-browser'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { DxSelectBoxModule, DxCheckBoxModule, DxTextBoxModule, DxDateBoxModule, DxDateRangeBoxModule, DxButtonModule, DxValidatorModule, DxValidatorComponent, DxValidationSummaryModule, } from 'devextreme-angular'; import notify from 'devextreme/ui/notify'; import { DxButtonTypes } from 'devextreme-angular/ui/button'; import { DxTextBoxTypes } from 'devextreme-angular/ui/text-box'; import { ValidationCallbackData } from 'devextreme-angular/common'; import { Service } from './app.service'; if (!/localhost/.test(document.location.host)) { enableProdMode(); } const sendRequest = function (value: string) { const invalidEmail = 'test@dx-email.com'; return new Promise((resolve) => { setTimeout(() => { resolve(value !== invalidEmail); }, 1000); }); }; @Component({ selector: 'demo-app', providers: [Service], templateUrl: 'app/app.component.html', styleUrls: ['app/app.component.css'], }) export class AppComponent { @ViewChild('targetValidator', { static: false }) validator: DxValidatorComponent; password = ''; confirmPassword = ''; passwordMode: DxTextBoxTypes.TextBoxType = 'password'; confirmPasswordMode: DxTextBoxTypes.TextBoxType = 'password'; maxDate = new Date(); cityPattern = '^[^0-9]+$'; namePattern = /^[^0-9]+$/; phonePattern = /^[02-9]\d{9}$/; countries: string[]; phoneRules: DxTextBoxTypes.Properties['maskRules'] = { X: /[02-9]/, }; passwordButton: DxButtonTypes.Properties = { icon: 'eyeopen', stylingMode: 'text', onClick: () => { this.passwordMode = this.passwordMode === 'text' ? 'password' : 'text'; }, }; confirmPasswordButton: DxButtonTypes.Properties = { icon: 'eyeopen', stylingMode: 'text', onClick: () => { this.confirmPasswordMode = this.confirmPasswordMode === 'text' ? 'password' : 'text'; }, }; constructor(service: Service) { this.maxDate = new Date(this.maxDate.setFullYear(this.maxDate.getFullYear() - 21)); this.countries = service.getCountries(); } passwordComparison = () => this.password; checkComparison = () => true; validateVacationDatesRange({ value: [startDate, endDate] }: ValidationCallbackData) { if (startDate === null || endDate === null) { return true; } const millisecondsPerDay = 24 * 60 * 60 * 1000; const daysDifference = Math.abs((endDate - startDate) / millisecondsPerDay); return daysDifference < 25; } validateVacationDatesPresence({ value: [startDate, endDate] }: ValidationCallbackData) { if (startDate === null && endDate === null) { return true; } return startDate !== null && endDate !== null; } asyncValidation = (params: ValidationCallbackData) => sendRequest(params.value); onPasswordChanged() { if (this.confirmPassword) { this.validator.instance.validate(); } } onFormSubmit = function (e: SubmitEvent) { notify({ message: 'You have submitted the form', position: { my: 'center top', at: 'center top', }, }, 'success', 3000); e.preventDefault(); }; } @NgModule({ imports: [ BrowserModule, BrowserTransferStateModule, DxSelectBoxModule, DxCheckBoxModule, DxTextBoxModule, DxDateBoxModule, DxDateRangeBoxModule, DxButtonModule, DxValidatorModule, DxValidationSummaryModule, ], declarations: [AppComponent], bootstrap: [AppComponent], }) export class AppModule { } platformBrowserDynamic().bootstrapModule(AppModule);
::ng-deep #summary { padding-left: 10px; margin-top: 20px; margin-bottom: 10px; }
import { Injectable } from '@angular/core'; const countries: string[] = [ 'Afghanistan', 'Albania', 'Algeria', 'Andorra', 'Angola', 'Antigua and Barbuda', 'Argentina', 'Armenia', 'Australia', 'Austria', 'Azerbaijan', 'The Bahamas', 'Bahrain', 'Bangladesh', 'Barbados', 'Belarus', 'Belgium', 'Belize', 'Benin', 'Bhutan', 'Bolivia', 'Bosnia and Herzegovina', 'Botswana', 'Brazil', 'Brunei', 'Bulgaria', 'Burkina Faso', 'Burma', 'Burundi', 'Cambodia', 'Cameroon', 'Canada', 'Cape Verde', 'Central African Republic', 'Chad', 'Chile', 'China', 'Colombia', 'Comoros', 'Democratic Republic of the Congo', 'Republic of the Congo', 'Costa Rica', 'Ivory Coast', 'Croatia', 'Cuba', 'Cyprus', 'Czech Republic', 'Denmark', 'Djibouti', 'Dominica', 'Dominican Republic', 'East Timor', 'Ecuador', 'Egypt', 'El Salvador', 'Equatorial Guinea', 'Eritrea', 'Estonia', 'Ethiopia', 'Fiji', 'Finland', 'France', 'Gabon', 'The Gambia', 'Georgia', 'Germany', 'Ghana', 'Greece', 'Grenada', 'Guatemala', 'Guinea', 'Guinea-Bissau', 'Guyana', 'Haiti', 'Honduras', 'Hungary', 'Iceland', 'India', 'Indonesia', 'Iran', 'Iraq', 'Republic of Ireland', 'Israel', 'Italy', 'Jamaica', 'Japan', 'Jordan', 'Kazakhstan', 'Kenya', 'Kiribati', 'North Korea', 'South Korea', 'Kuwait', 'Kyrgyzstan', 'Laos', 'Latvia', 'Lebanon', 'Lesotho', 'Liberia', 'Libya', 'Liechtenstein', 'Lithuania', 'Luxembourg', 'Republic of Macedonia', 'Madagascar', 'Malawi', 'Malaysia', 'Maldives', 'Mali', 'Malta', 'Marshall Islands', 'Mauritania', 'Mauritius', 'Mexico', 'Federated States of Micronesia', 'Moldova', 'Monaco', 'Mongolia', 'Montenegro', 'Morocco', 'Mozambique', 'Namibia', 'Nauru', 'Nepal', 'Kingdom of the Netherlands', 'New Zealand', 'Nicaragua', 'Niger', 'Nigeria', 'Norway', 'Oman', 'Pakistan', 'Palau', 'State of Palestine', 'Panama', 'Papua New Guinea', 'Paraguay', 'Peru', 'Philippines', 'Poland', 'Portugal', 'Qatar', 'Romania', 'Russia', 'Rwanda', 'Saint Kitts and Nevis', 'Saint Lucia', 'Saint Vincent and the Grenadines', 'Samoa', 'San Marino', 'São Tomé and Príncipe', 'Saudi Arabia', 'Senegal', 'Serbia', 'Seychelles', 'Sierra Leone', 'Singapore', 'Slovakia', 'Slovenia', 'Solomon Islands', 'Somalia', 'South Africa', 'South Sudan', 'Spain', 'Sri Lanka', 'Sudan', 'Suriname', 'Swaziland', 'Sweden', 'Switzerland', 'Syria', 'Tajikistan', 'Tanzania', 'Thailand', 'Togo', 'Tonga', 'Trinidad and Tobago', 'Tunisia', 'Turkey', 'Turkmenistan', 'Tuvalu', 'Uganda', 'Ukraine', 'United Arab Emirates', 'United Kingdom', 'United States', 'Uruguay', 'Uzbekistan', 'Vanuatu', 'Vatican City', 'Venezuela', 'Vietnam', 'Yemen', 'Zambia', 'Zimbabwe']; @Injectable() export class Service { getCountries() { return countries; } }
// In real applications, you should not transpile code in the browser. // You can see how to create your own application with Angular and DevExtreme here: // https://js.devexpress.com/Documentation/Guide/Angular_Components/Getting_Started/Create_a_DevExtreme_Application/ window.exports = window.exports || {}; window.config = { transpiler: 'ts', typescriptOptions: { module: 'system', emitDecoratorMetadata: true, experimentalDecorators: true, }, meta: { 'typescript': { 'exports': 'ts', }, 'devextreme/time_zone_utils.js': { 'esModule': true, }, 'devextreme/localization.js': { 'esModule': true, }, 'devextreme/viz/palette.js': { 'esModule': true, }, }, paths: { 'npm:': 'https://unpkg.com/', }, map: { 'ts': 'npm:plugin-typescript@4.2.4/lib/plugin.js', 'typescript': 'npm:typescript@4.2.4/lib/typescript.js', '@angular/core': 'npm:@angular/core@12.2.17', '@angular/platform-browser': 'npm:@angular/platform-browser@12.2.17', '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic@12.2.17', '@angular/forms': 'npm:@angular/forms@12.2.17', '@angular/common': 'npm:@angular/common@12.2.17', '@angular/compiler': 'npm:@angular/compiler@12.2.17', 'tslib': 'npm:tslib@2.6.2/tslib.js', 'rxjs': 'npm:rxjs@7.5.3/dist/bundles/rxjs.umd.js', 'rxjs/operators': 'npm:rxjs@7.5.3/dist/cjs/operators/index.js', 'rrule': 'npm:rrule@2.6.4/dist/es5/rrule.js', 'luxon': 'npm:luxon@1.28.1/build/global/luxon.min.js', 'es6-object-assign': 'npm:es6-object-assign@1.1.0', 'devextreme': 'npm:devextreme@23.2.5/cjs', 'devextreme/bundles/dx.all': 'npm:devextreme@23.2.5/bundles/dx.all.js', 'jszip': 'npm:jszip@3.10.1/dist/jszip.min.js', 'devextreme-quill': 'npm:devextreme-quill@1.6.4/dist/dx-quill.min.js', 'devexpress-diagram': 'npm:devexpress-diagram@2.2.5', 'devexpress-gantt': 'npm:devexpress-gantt@4.1.51', 'devextreme-angular': 'npm:devextreme-angular@23.2.5', '@devextreme/runtime': 'npm:@devextreme/runtime@3.0.12', 'inferno': 'npm:inferno@7.4.11/dist/inferno.min.js', 'inferno-compat': 'npm:inferno-compat/dist/inferno-compat.min.js', 'inferno-create-element': 'npm:inferno-create-element@7.4.11/dist/inferno-create-element.min.js', 'inferno-dom': 'npm:inferno-dom/dist/inferno-dom.min.js', 'inferno-hydrate': 'npm:inferno-hydrate@7.4.11/dist/inferno-hydrate.min.js', 'inferno-clone-vnode': 'npm:inferno-clone-vnode/dist/inferno-clone-vnode.min.js', 'inferno-create-class': 'npm:inferno-create-class/dist/inferno-create-class.min.js', 'inferno-extras': 'npm:inferno-extras/dist/inferno-extras.min.js', // Prettier 'prettier/standalone': 'npm:prettier@2.8.4/standalone.js', 'prettier/parser-html': 'npm:prettier@2.8.4/parser-html.js', }, packages: { 'app': { main: './app.component.ts', defaultExtension: 'ts', }, 'devextreme': { defaultExtension: 'js', }, 'devextreme/events/utils': { main: 'index', }, 'devextreme/events': { main: 'index', }, 'es6-object-assign': { main: './index.js', defaultExtension: 'js', }, 'rxjs': { defaultExtension: 'js', }, 'rxjs/operators': { defaultExtension: 'js', }, }, packageConfigPaths: [ 'npm:@devextreme/*/package.json', 'npm:@devextreme/runtime@3.0.12/inferno/package.json', 'npm:@angular/*/package.json', 'npm:@angular/common@12.2.17/*/package.json', 'npm:rxjs@7.5.3/package.json', 'npm:rxjs@7.5.3/operators/package.json', 'npm:devextreme-angular@23.2.5/*/package.json', 'npm:devextreme-angular@23.2.5/ui/*/package.json', 'npm:devextreme-angular@23.2.5/package.json', 'npm:devexpress-diagram@2.2.5/package.json', 'npm:devexpress-gantt@4.1.51/package.json', ], }; System.config(window.config);
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>DevExtreme Demo</title> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" /> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/23.2.5/css/dx.light.css" /> <script src="https://unpkg.com/core-js@2.6.12/client/shim.min.js"></script> <script src="https://unpkg.com/zone.js@0.12.0/dist/zone.js"></script> <script src="https://unpkg.com/reflect-metadata@0.1.13/Reflect.js"></script> <script src="https://unpkg.com/systemjs@0.21.3/dist/system.js"></script> <script src="config.js"></script> <script> System.import("app").catch(console.error.bind(console)); </script> </head> <body class="dx-viewport"> <div class="demo-container"> <demo-app>Loading...</demo-app> </div> </body> </html>