JavaScript/jQuery Common - Data Validation
This guide provides the detailed information on validation capabilities of DevExtreme editors. It describes how to validate a single editor or a group of editors, display the validation summary, perform remote validation, use a custom validation engine, etc.
Validate an Editor Value
Associate a DevExtreme editor with the Validator UI component and specify validationRules to validate the editor. The full list of predefined validation rules is available in the Validation Rules Reference section.
- $(function () {
- $("#login").dxTextBox({
- placeholder: 'Login'
- }).dxValidator({
- validationRules: [{
- type: 'required'
- }, {
- type: 'pattern',
- pattern: '^[a-zA-Z]+$',
- message: 'Do not use digits.'
- }]
- });
- });
- <div id="login"></div>
Group the Editors
Editors belonging to a single Validation Group can be validated together. All editors on a page are automatically collected in a Default Validation Group, which is suitable when you do not need to validate collections of editors separately. In other cases, define Validation Groups as shown in the following code:
- $(function () {
- var loginGroup = "loginGroup";
- $("#login")
- .dxTextBox({ /* ... */ })
- .dxValidator({
- // ...
- validationGroup: loginGroup
- });
- $("#password")
- .dxTextBox({ /* ... */ })
- .dxValidator({
- // ...
- validationGroup: loginGroup
- });
- });
- <div id="login"></div>
- <div id="password"></div>
Validate the Group
Call a group's validate() method in a Button's onClick event handler to validate the group. You can access the Validation Group via the handler's argument. The Button always validates the group to which it belongs. If the membership is not specified, the Button validates the Default Validation Group.
- $(function () {
- // var loginGroup = "loginGroup";
- $("#login")
- .dxTextBox({ /* ... */ })
- .dxValidator({
- // validationGroup: loginGroup,
- validationRules: [ /* ... */ ]
- });
- $("#password")
- .dxTextBox({ /* ... */ })
- .dxValidator({
- // validationGroup: loginGroup,
- validationRules: [ /* ... */ ]
- });
- $("#loginButton").dxButton({
- text: "Sign in",
- // validationGroup: loginGroup
- onClick: function (e) {
- var result = e.validationGroup.validate();
- if (result.isValid) {
- // ...
- }
- }
- });
- });
- <div id="login"></div>
- <div id="password"></div>
- <div id="loginButton"></div>
Alternatively, you can use the DevExpress.validationEngine.validateGroup method to validate a group.
- $(function() {
- $("#login").dxTextBox({})
- .dxValidator({
- validationGroup: "loginGroup",
- validationRules: [{
- type: "required"
- }]
- });
- $("#password").dxTextBox({})
- .dxValidator({
- validationGroup: "loginGroup",
- validationRules: [{
- type: "required"
- }]
- });
- $("#button").dxButton({
- text: "Sign in",
- onClick: function() {
- DevExpress.validationEngine.validateGroup("loginGroup");
- }
- });
- });
Display Validation Errors
All group validation errors can be displayed in the ValidationSummary UI component. The following code shows how to add this UI component to a page. The commented-out codelines associate the Validation Summary with a named Validation Group.
- $(function () {
- // var loginGroup = "loginGroup";
- $("#summary").dxValidationSummary({
- // validationGroup: loginGroup
- });
- });
- ...
- <div id="summary"></div>
Disable Validation Dynamically
All the rules, except the CustomRule and AsyncRule, are always applied and cannot be disabled at runtime.
If you need to disable validation dynamically, implement a CustomRule or AsyncRule in which you should simulate the validation logic of a target rule but apply it only under certain conditions.
The following example illustrates this case. A page contains two TextBoxes and a CheckBox. The first TextBox has proper RequiredRule; the second TextBox has a CustomRule that simulates the RequiredRule logic but applies it only when the CheckBox is selected. The reevaluate property is enabled to re-check the TextBox value after the CheckBox value was changed.
- $(function() {
- $("#firstName").dxTextBox({ })
- .dxValidator({
- validationRules: [{
- type: "required"
- }]
- });
- $("#lastName").dxTextBox({ })
- .dxValidator({
- validationRules: [{
- type: "custom",
- message: "Required",
- reevaluate: true,
- validationCallback: function(e) {
- if ($("#checkBox").dxCheckBox("option", "value")) {
- return !!e.value;
- }
- return true;
- }
- }]
- });
- $("#validationButton").dxButton({
- text: "Validate",
- onClick: function (params) {
- params.validationGroup.validate();
- }
- });
- $("#checkBox").dxCheckBox({
- text: "Validate last name",
- value: false
- });
- });
Custom Validation
To implement custom validation, use the CustomRule. Refer to the validationCallback function's description for an example.
Server-Side Validation
To implement server-side validation, use the AsyncRule. Refer to the validationCallback function's description for an example.
Validate a Custom Value
You can use the DevExtreme validation engine to validate a custom value, for example, a non-DevExtreme editor value or a concatenation of several editor values, by configuring the Validator's adapter property. The following example creates two text boxes and a button. A button click checks that at least one of these text boxes is filled. Their values are provided by the getValue function.
- $(function() {
- var callbacks = [];
- var revalidate = function() {
- callbacks.forEach(func => {
- func();
- });
- }
- var phone = $("#phone").dxTextBox({
- placeholder: "Phone",
- onValueChanged: revalidate
- }).dxTextBox("instance");
- var email = $("#email").dxTextBox({
- type: "email",
- placeholder: "Email",
- onValueChanged: revalidate
- }).dxTextBox("instance");
- $("#validator").dxValidator({
- validationRules: [{
- type: "required",
- message: "Specify your phone or email."
- }],
- adapter: {
- getValue: function() {
- return phone.option("value") || email.option("value");
- },
- applyValidationResults: function(e) {
- $("#contacts").css({ "border": e.isValid ? "none" : "1px solid red" });
- },
- validationRequestsCallbacks: callbacks
- }
- });
- $("#button").dxButton({
- text: "Contact me",
- onClick: function(e) {
- const { isValid } = e.validationGroup.validate();
- if (isValid) {
- // Submit values to the server
- }
- }
- });
- $("#summary").dxValidationSummary({ });
- });
- <div id="contacts">
- <div id="phone"></div>
- <div id="email"></div>
- </div>
- <div id="validator"></div>
- <div id="summary"></div>
- <div id="button"></div>
If you have technical questions, please create a support ticket in the DevExpress Support Center.