JavaScript/jQuery Form - Validate and Submit the Form
The Form UI component uses the built-in validation engine to validate form item values. You can attach validation rules to a simple item using its validationRules property when you create items explicitly...
- $(function() {
- $("#formContainer").dxForm({
- formData: {
- firstName: "John",
- lastName: "Heart"
- },
- items: [{
- dataField: "firstName",
- validationRules: [{
- type: "required",
- message: "First Name is required"
- }, {
- type: "pattern",
- pattern: "^[a-zA-Z]+$",
- message: "The name should not contain digits"
- }]
- },
- // ...
- ]
- });
- });
... or when you customize automatically generated items.
- $(function() {
- $("#formContainer").dxForm({
- formData: {
- firstName: "John",
- lastName: "Heart"
- },
- customizeItem: function(item) {
- if(item.dataField === "FirstName" || item.dataField === "LastName") {
- item.validationRules = [{
- type: "required",
- message: "The value is required"
- }, {
- type: "pattern",
- pattern: "^[a-zA-Z]+$",
- message: "The value should not contain digits"
- }]
- }
- }
- });
- });
A single Form editor is validated individually once its value changes. If the value fails to pass the validation check, the editor displays an error message. Note that you can also call the validate() method to validate all Form editors simultaneously. In this case, the Form can display all validation errors at the bottom if you set the showValidationSummary property to true.
Usually, Form editors should be submitted to the server after being successfully validated on the client. The following code shows how to do this using a button form item. Note that the Form UI component is wrapped in the <form>
tag in the markup.
- <form action="/Login" method="post">
- <div id="formWidget"></div>
- </form>
- $(function () {
- $("#formWidget").dxForm({
- // ...
- validationGroup: "groupName",
- items: [{
- itemType: "button",
- buttonOptions: {
- text: "Submit the Form",
- useSubmitBehavior: true
- }
- },
- // ...
- ]
- });
- });