Your search did not match any results.
Form

Validation

This demo shows how to validate Form editors. To apply validation rules to an editor, declare them in the validationRules[] array. Specify type and other properties for each rule.

The following validation rules are shown in this demo:

  • RequiredRule
    Requires that a validated editor has a value.

  • CompareRule
    Compares the editor's value to the specified expression.

  • PatternRule
    Checks whether an editor value matches a specified pattern.

  • RangeRule
    Checks whether an editor value is in a specified range.

  • StringLengthRule
    Requires that an editor value length is in a specified range.

  • EmailRule
    Requires that an editor value matches the Email pattern.

  • AsyncRule
    Allows you to add custom server-side validation logic. Rules of this type run last, only if all other rules passed. In this demo, an AsyncRule checks whether user input matches test@dx-email.com.

To submit form data, do the following:

  1. Wrap the Form component in the HTML <form> element.

  2. Use the Button Form Item to add a button to the form. This button submits the form data.

  3. Enable the button's useSubmitBehavior property.

When users click the button, the Form validates all editors that belong to the same validationGroup as this button. In this demo, all these editors belong to the customerData group. Form data can be submitted to a server only if input validation is successful. Enable the showValidationSummary property to display all validation errors at the bottom of the Form.

Copy to CodePen
Apply
Reset
$(function(){ var sendRequest = function(value) { var validEmail = "test@dx-email.com"; var d = $.Deferred(); setTimeout(function() { d.resolve(value === validEmail); }, 1000); return d.promise(); } var formWidget = $("#form").dxForm({ formData: formData, readOnly: false, showColonAfterLabel: true, showValidationSummary: true, validationGroup: "customerData", items: [{ itemType: "group", caption: "Credentials", items: [{ dataField: "Email", validationRules: [{ type: "required", message: "Email is required" }, { type: "email", message: "Email is invalid" }, { type: "async", message: "Email is already registered", validationCallback: function(params) { return sendRequest(params.value); } }] }, { dataField: "Password", editorOptions: { mode: "password" }, validationRules: [{ type: "required", message: "Password is required" }] }, { label: { text: "Confirm Password" }, editorType: "dxTextBox", editorOptions: { mode: "password" }, validationRules: [{ type: "required", message: "Confirm Password is required" }, { type: "compare", message: "'Password' and 'Confirm Password' do not match", comparisonTarget: function() { return formWidget.option("formData").Password; } }] }] }, { itemType: "group", caption: "Personal Data", items: [{ dataField: "Name", validationRules: [{ type: "required", message: "Name is required" }, { type: "pattern", pattern: "^[^0-9]+$", message: "Do not use digits in the Name" }] }, { dataField: "Date", editorType: "dxDateBox", label: { text: "Date of birth" }, editorOptions: { invalidDateMessage: "The date must have the following format: MM/dd/yyyy" }, validationRules: [{ type: "required", message: "Date of birth is required" }, { type: "range", max: new Date().setFullYear(new Date().getFullYear() - 21), message: "You must be at least 21 years old" }] }] }, { itemType: "group", caption: "Billing address", items: [{ dataField: "Country", editorType: "dxSelectBox", editorOptions: { dataSource: countries }, validationRules: [{ type: "required", message: "Country is required" }] }, { dataField: "City", editorType: "dxAutocomplete", editorOptions: { dataSource: cities, minSearchLength: 2 }, validationRules: [{ type: "pattern", pattern: "^[^0-9]+$", message: "Do not use digits in the City name" }, { type: "stringLength", min: 2, message: "City must have at least 2 symbols" }, { type: "required", message: "City is required" }] }, { dataField: "Address", validationRules: [{ type: "required", message: "Address is required" }] }, { dataField: "Phone", helpText: "Enter the phone number in USA phone format", editorOptions: { mask: "+1 (X00) 000-0000", maskRules: { "X": /[02-9]/ }, maskInvalidMessage: "The phone must have a correct USA phone format", useMaskedValue: true }, validationRules: [{ type: "pattern", pattern: /^\+\s*1\s*\(\s*[02-9]\d{2}\)\s*\d{3}\s*-\s*\d{4}$/, message: "The phone must have a correct USA phone format" }] }, { dataField: "Accepted", label: { visible: false }, editorOptions: { text: "I agree to the Terms and Conditions" }, validationRules: [{ type: "compare", comparisonTarget: function() { return true; }, message: "You must agree to the Terms and Conditions" }] }] }, { itemType: "button", horizontalAlignment: "left", buttonOptions: { text: "Register", type: "success", useSubmitBehavior: true } }] }).dxForm("instance"); $("#form-container").on("submit", function(e) { DevExpress.ui.notify({ message: "You have submitted the form", position: { my: "center top", at: "center top" } }, "success", 3000); e.preventDefault(); }); });
<!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" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script>window.jQuery || document.write(decodeURIComponent('%3Cscript src="js/jquery.min.js"%3E%3C/script%3E'))</script> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/21.1.4/css/dx.common.css" /> <link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/21.1.4/css/dx.light.css" /> <script src="https://cdn3.devexpress.com/jslib/21.1.4/js/dx.all.js"></script> <script src="data.js"></script> <link rel="stylesheet" type="text/css" href="styles.css" /> <script src="index.js"></script> </head> <body class="dx-viewport"> <div class="demo-container"> <form action="your-action" id="form-container"> <div id="form"></div> </form> </div> </body> </html>
form { margin: 10px; }
var formData = { "Email": "", "Password": "", "Name": "Peter", "Date": null, "Country": "", "City": "", "Address": "", "Phone": "", "Accepted": false }; var countries = ['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']; var cities = ["New York", "Los Angeles", "Chicago", "Houston", "Philadelphia", "Phoenix", "San Antonio", "San Diego", "Dallas", "San Jose", "Austin", "Indianapolis", "Jacksonville", "San Francisco", "Columbus", "Charlotte", "Fort Worth", "Detroit", "El Paso", "Memphis", "Seattle", "Denver", "Washington", "Boston", "Nashville", "Baltimore", "Oklahoma City", "Louisville", "Portland", "Las Vegas", "Milwaukee", "Albuquerque", "Tucson", "Fresno", "Sacramento", "Long Beach", "Kansas City", "Mesa", "Virginia Beach", "Atlanta", "Colorado Springs", "Omaha", "Raleigh", "Miami", "Oakland", "Minneapolis", "Tulsa", "Cleveland", "Wichita", "Arlington", "New Orleans", "Bakersfield", "Tampa", "Honolulu", "Aurora", "Anaheim", "Santa Ana", "St. Louis", "Riverside", "Corpus Christi", "Lexington", "Pittsburgh", "Anchorage", "Stockton", "Cincinnati", "Saint Paul", "Toledo", "Greensboro", "Newark", "Plano", "Henderson", "Lincoln", "Buffalo", "Jersey City", "Chula Vista", "Fort Wayne", "Orlando", "St. Petersburg", "Chandler", "Laredo", "Norfolk", "Durham", "Madison", "Lubbock", "Irvine", "Winston–Salem", "Glendale", "Garland", "Hialeah", "Reno", "Chesapeake", "Gilbert", "Baton Rouge", "Irving", "Scottsdale", "North Las Vegas", "Fremont", "Boise", "Richmond"];