The namespace System.ComponentModel.DataAnnotations, has a group of classes, attributes and methods, to make validations in our .NET applications.
In the Microsoft world, there are technologies such as WPF, Silverlight, ASP MVC, Entity Framework, etc., which make automatic validation with class and exclusive attributes. We think this mechanism is exclusive of these technologies, but it is not like this. We can use it with all classes of Framework.
Add restriction to our classes
The way of adding restrictions to our classes is by attributes in the properties.
- public class Customer
- {
- [Required]
- public string Name { get; set; }
- public DateTime EntryDate { get; set; }
- public string Password { get; set; }
- public string PasswordConfirmation { get; set; }
- }
In this example, we have used a Required attribute, but there are more, as we will discuss later.
Validation Attributes Available
All attributes in this section inherits of abstract class ValidationAttribute.
- ValidationAttribute validate only one Property in the object.
- ValidationAttribute, has an important property, ErrorMessage. This property get or set the custom validation message in case of error.
- ErrorMessage has an implicit ‘FormatString’, like System.Console.Write or System.Console.WriteLine methods, concerning the use of "{0}{1}{2} … {n}" parameters.
- public class TestClass
- {
- public string PropertyOne { get; set; }
- [MaxLength(2, ErrorMessage = "The property {0} doesn't have more than {1} elements")]
- public int[] ArrayInt { get; set; }
- }
This is the error validation message.

The sequence of parameters (for ‘StringFormat’) will be the next.
{0} - PropertyName
{1} - Parameter 1
{2} - Parameter 2
…
{n} - Parameter n
We will study the list of validation attributes available.
CompareAttribute
This attribute, compares the value of two properties. More info in Link.
- public class Customer
- {
- public string Name { get; set; }
- public DateTime EntryDate { get; set; }
- public string Password { get; set; }
- [Compare("Customer.Password", ErrorMessage = "The fields Password and PasswordConfirmation should be equals")]
- public string PasswordConfirmation { get; set; }
- }
This attribute, compares the property marked, with the property linked in its first parameter by a string argument.
DateTypeAttribute
This attribute allows marking one property/field way more specifically than .Net types. MSDN says: DateTypeAttribute specifies the name of an additional type to associate with a data field. Link.
In applications (ASP MVC, Silverlight, etc), with templates, can be used to changed display data format. For example one property market with DateTypeAttribute to Password, show its data in a TextBox with “*” character.
- public class Customer
- {
- public string Name { get; set; }
- public DateTime EntryDate { get; set; }
- [DataType(DataType.Password)]
- public string Password { get; set; }
- public string PasswordConfirmation { get; set; }
- }
The enum DataType has the following values.
- Custom = 0
- DateTime = 1,
- Date = 2,
- Time = 3,
- Duration = 4,
- PhoneNumber = 5,
- Currency = 6,
- Text = 7,
- Html = 8,
- MultilineText = 9,
- EmailAddress = 10,
- Password = 11,
- Url = 12,
- ImageUrl = 13,
- CreditCard = 14,
- PostalCode = 15,
- Upload = 16,
StringLenghtAttribute
Marked the max and the min length of characters allowed in the property/field. Link.
- public class Customer
- {
- [StringLength(maximumLength: 50 , MinimumLength = 10,
- ErrorMessage = "The property {0} should have {1} maximum characters and {2} minimum characters")]
- public string Name { get; set; }
- public DateTime EntryDate { get; set; }
- public string Password { get; set; }
- public string PasswordConfirmation { get; set; }
- }
MaxLengthAttribute and MinLengthAttribute
These attributes were added to Entity Framework 4.1.
Specify the number of maximum and minimum elements in the Array property. This is valid for string properties, because one string is a char[] too. More information (MaxLengthAttribute, MinLengthAttribute).
In this example, we can see the two types: for string and for array.
- public class Customer
- {
- [MaxLength(50, ErrorMessage = "The {0} can not have more than {1} characters")]
- public string Name { get; set; }
- public DateTime EntryDate { get; set; }
- [DataType(DataType.Password)]
- public string Password { get; set; }
- [Compare("Customer.Password", ErrorMessage = "The fields Password and PasswordConfirmation should be equals")]
- public string PasswordConfirmation { get; set; }
- [MaxLength(2, ErrorMessage = "The property {0} can not have more than {1} elements")]
- public int[] EjArrayInt { get; set; }
- }
In the case of Array property, this properties should be Array, are not valid: List<T>, Collection<T>, etc.
RequieredAttribute
Specify that the field is mandatory and it doesn’t contain a null or string.Empty values. Link.
- public class Customer
- {
- [Required (ErrorMessage = "{0} is a mandatory field")]
- public string Name { get; set; }
- public DateTime EntryDate { get; set; }
- public string Password { get; set; }
- public string PasswordConfirmation { get; set; }
- }
RangeAttribute
Specify a range value for a data field. Link.
- public class Customer
- {
- public string Name { get; set; }
- [Range(typeof(DateTime), "01/01/1900", "01/01/2014",
- ErrorMessage = "Valid dates for the Property {0} between {1} and {2}")]
- public DateTime EntryDate { get; set; }
- public string Password { get; set; }
- [Compare("Customer.Password", ErrorMessage = "The fields Password and PasswordConfirmation should be equals")]
- public string PasswordConfirmation { get; set; }
- [Range(0, 150, ErrorMessage = "The Age should be between 0 and 150 years")]
- public int Age { get; set; }
- }


Bjørn HellesyltPosted Feb 8, 2022, 8:27 AM
Hi. Do you have any suggestions on how to set a validation error message on a Url DataType? It is not a required field. The default message is to enter a valid Url. Some people do not understand that this means adding HTTPs:// as well.
HxongPosted Sep 10, 2021, 3:52 AM
Hi, thank you for the post, is it possible to dynamically add attributes like Validation Attribute or Regular Expression Attribute to a dynamically created object?
Upendra Pratap ShahiPosted Dec 8, 2016, 10:34 AM
Nice one explaination, thanks for sharing...