Validation using Data Annotation Attributes
ASP.NET MVC includes built-in attribute classes in the System.ComponentModel.DataAnnotations namespace. These attributes are used to define metadata for ASP.NET MVC and ASP.NET data controls. You can apply these attributes to the properties of the model class to display appropriate validation messages to the users.
The following table lists all the data annotation attributes which can be used for validation.
Attribute | Usage |
---|---|
Required | Specifies that a property value is required. |
StringLength | Specifies the minimum and maximum length of characters that are allowed in a string type property. |
Range | Specifies the numeric range constraints for the value of a property. |
RegularExpression | Specifies that a property value must match the specified regular expression. |
CreditCard | Specifies that a property value is a credit card number. |
CustomValidation | Specifies a custom validation method that is used to validate a property. |
EmailAddress | Validates an email address. |
FileExtension | Validates file name extensions. |
MaxLength | Specifies the maximum length of array or string data allowed in a property. |
MinLength | Specifies the minimum length of array or string data allowed in a property. |
Phone | Specifies that a property value is a well-formed phone nu |