Supported Attributes for Model Validation
- 4 minutes to read
DevExpress MVC Extensions support a model-based data validation approach that is based on decorating model class properties with validation attributes.
The System.ComponentModel.DataAnnotations namespace contains the following attributes which you can use within the DevExpress MVC Extensions to enforce validation rules for data applied to a class or member:
Validation Attribute | Description |
---|---|
CreditCardAttribute | Specifies that a data field value is a credit card number. |
EmailAddressAttribute | Validates an email address. |
FileExtensionsAttribute | Validates file name extensions. |
MaxLengthAttribute | Specifies the maximum length of array or string data allowed in a property. |
MinLengthAttribute | Specifies the minimum length of array of string data allowed in a property. |
PhoneAttribute | Specifies that a data field value is a well-formed phone number using a regular expression for phone numbers. |
RangeAttribute | Specifies the numeric range constraints for the value of a data field. |
RegularExpressionAttribute | Specifies that a data field value in ASP.NET Dynamic Data must match the specified regular expression. |
RequiredAttribute | Specifies that a data field value is required. |
StringLengthAttribute | Specifies the minimum and maximum length of characters that are allowed in a data field. |
UrlAttribute | Provides URL validation. |
The model-based validation allows you to validate user-input on the server when an end-user adds or edits data via the GridView or TreeList. DevExpress MVC Data Editors support client and server-side validation. Refer to this help topic to learn more: Validation Overview.
Remote Validation
ASP.NET MVC 3 provides a remote validation approach: a mechanism that can make a remote server call in order to validate a form field without posting the entire form to the server. This validation approach is implemented using the RemoteAttribute from System.Web.Mvc namespace. DevExpress MVC Data Editors support the remote validation approach and the [Remote] attribute. Refer to the Remote Validation topic to learn more.
This demo shows how to implement remote validation with the [Remote] attribute and jQuery: jQuery Client Validation.
Review this MSDN topic to learn more about remote validation: How to: Implement Remote Validation in ASP.NET MVC.
DevExpress Mask Attribute
The DevExpress ASP.NET MVC editors allow you to use masks during editing. Masks are useful when a string entered by an end-user into an editor must match a specific format. For instance, a text editor that accepts date/time values in the 24-hour format only, or only numeric values, or a phone number that only allows an end-user to enter digits into automatically constructed placeholders.
You can define the mask settings for a model field using the DevExpress Mask attribute and the mask pattern will automatically be displayed within the text area of the DevExpress MVC Data Editors. The Mask attribute enables the validation mechanism that checks whether the entered value matches the mask expression. The decorated model field will be automatically validated on the server side.
Note
The DevExpress Mask attribute enables validation of user input on the server side. If you use any other third-party text editor, like the standard Html.TextBoxFor helper, the server-side validation mechanism is still triggered.
Only the DevExpress ASP.NET MVC Data Editors can retrieve mask settings defined using the DevExpress Mask attribute from the metadata and display a mask pattern in the edit area.
The Mask attribute is implemented by the MaskAttribute class.
The code sample below demonstrates how to decorate a model field with the Mask attribute.
namespace MyProject.Models
{
using DevExpress.Web.Mvc;
using System.ComponentModel.DataAnnotations;
public class Customer
{
// ...
// Validates whether the input value matches the mask expression.
[Mask("+1 (999) 000-0000", ErrorMessage = "The phone number is not valid!")]
[Required]
public string Phone { get; set; }
}
}
The image below illustrates the look of a DevExpress ASP.NET MVC TextBox extension bound to a field decorated with a DevExpress Mask attribute as shown in a code sample above.