Introduction
In our earlier entries in this series on securing web applications, we introduced data annotations as rules defined within the model class. We demonstrated their application using simple annotations on the Customer class. In this post, we will define data annotations and explore common types that you can effectively utilize in your applications.
Data annotations are a set of attributes in ASP.NET Core MVC that you can apply to model properties to specify validation rules, formatting options, display options, and more. These attributes help you define the behavior of your models and enforce data integrity in your application.
Common Data Annotations
Here’s a comprehensive list of commonly used data annotation attributes in ASP.NET Core MVC, along with descriptions of their usage:
| Attributes | Description | Usage |
| [Required] | Specifies that a property is required and must have a non-null value. | [Required] |
| [StringLength] | Specifies the minimum and maximum length constraints for strings. Parameters: MinimumLength: Specifies the minimum length of the string. MaximumLength: Specifies the maximum length of the string. | [StringLength(50, MinimumLength = 2)] |
| [Range] | Specifies numeric range constraints for integer and floating-point properties. Parameters: Minimum: Specifies the minimum allowed value. Maximum: Specifies the maximum allowed value. | [Range(1, 100)] |
| [RegularExpression] | Specifies a regular expression pattern that the property value must match. Parameters: Pattern: Specifies the regular expression pattern. | [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$")] |
| [EmailAddress] | Specifies that the property value must be a valid email address. | [EmailAddress] |
| [DataType] | Specifies the data type of the property. Parameters: DataType: Specifies the data type (e.g., DataType.Date, DataType.Time, DataType.PhoneNumber). | [DataType(DataType.Date)] |
| [Display] | Specifies display-related metadata for the property. Parameters: Name: Specifies the display name for the property. | [Display(Name = "First Name")] |
| [DisplayFormat] | Specifies formatting options for the property value. Parameters: DataFormatString: Specifies the format string for formatting the property value. | [DisplayFormat(DataFormatString = "{0:C}")] |
| [Url] | Specifies that the property value must be a valid URL. | [Url] |
| [MinLength] | Specifies the minimum length of a string or array. Parameters: Length: Specifies the minimum length. | [MinLength(2)] |
| [MaxLength] | : Specifies the allowed file extensions for file upload properties. Parameters: Extensions: Specifies the allowed file extensions. | [MaxLength(100)] |
| [FileExtensions] | Specifies the allowed file extensions for file upload properties. Parameters: Extensions: Specifies the allowed file extensions. | [FileExtensions(Extensions = ".jpg,.jpeg,.png")] |
| [DataType(DataType.Password)] | Specifies that the property represents a password field. | [DataType(DataType.Password)] |
| [Compare] | Specifies that the property value must match the value of another property. Parameters: OtherProperty: Specifies the name of the other property to compare against. | [Compare("Password")] |
Conclusion
In conclusion, our exploration of ASP.NET validation continues as we focus on a range of common data attributes. There may be instances where you need to go beyond the standard attributes provided out of the box. In our upcoming post, we will delve into how to extend these predefined annotation attributes to better suit your specific requirements.
Meanwhile, we greatly value your feedback and are eager to hear your thoughts on this discussion. Please feel free to comment below with any feedback or suggest topics you’d like us to explore in future posts. Your insights and experiences are vital in enriching our content and making this series a comprehensive resource for developers everywhere.

Good info. Lucky me I came across your website by accident (stumbleupon). I’ve book-marked it for later!
Whoa! This blog looks just like my old one! It’s on a completely different subject but it has pretty much the same layout and design. Excellent choice of colors!
Hello there, I found your website via Google while searching for a related topic, your website came up, it looks great. I have bookmarked it in my google bookmarks.