Introduction
Data annotations in .NET offer developers a convenient way to define validation rules and presentation attributes for model classes. By annotating properties with attributes from the System.ComponentModel.DataAnnotations namespace, developers can enforce data integrity and improve user interaction. This article explores 17 commonly used data annotations in .NET, providing detailed examples for each.
Required Attribute
The [Required] attribute ensures that a property must have a non-null value. It’s commonly used to enforce the presence of essential data fields.
Example
public class Person
{
[Required(ErrorMessage = "First name is required.")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last name is required.")]
public string LastName { get; set; }
}
StringLength Attribute
The [StringLength] attribute specifies the maximum and minimum lengths of a string property. It’s useful for enforcing length constraints on string values.
Example
public class Product
{
[StringLength(50, MinimumLength = 3, ErrorMessage = "Product name must be between 3 and 50 characters.")]
public string Name { get; set; }
}
Range Attribute
The [Range] attribute validates that a numeric property falls within a specified range of values.
Example
public class Product
{
[Range(0, 100, ErrorMessage = "Price must be between 0 and 100.")]
public decimal Price { get; set; }
}
RegularExpression Attribute
The [RegularExpression] attribute specifies a regular expression pattern that a string property must match.
Example
public class Customer
{
[RegularExpression(@"^\d{3}-\d{3}-\d{4}$", ErrorMessage = "Invalid phone number format.")]
public string PhoneNumber { get; set; }
}
EmailAddress Attribute
The [EmailAddress] attribute validates that a string property contains a valid email address.
Example
public class Contact
{
[EmailAddress(ErrorMessage = "Invalid email address.")]
public string Email { get; set; }
}
Phone Attribute
The [Phone] attribute validates that a string property contains a valid phone number.
Example
public class Contact
{
[Phone(ErrorMessage = "Invalid phone number.")]
public string PhoneNumber { get; set; }
}
Url Attribute
The [Url] attribute validates that a string property contains a valid URL.
Example
public class Website
{
[Url(ErrorMessage = "Invalid URL.")]
public string Url { get; set; }
}
CreditCard Attribute
The [CreditCard] attribute validates that a string property contains a valid credit card number.
Example
public class Payment
{
[CreditCard(ErrorMessage = "Invalid credit card number.")]
public string CardNumber { get; set; }
}
DataType Attribute
The [DataType] attribute specifies the data type of a property.
Example
public class Product
{
[DataType(DataType.Currency)]
public decimal Price { get; set; }
}
Compare Attribute
The [Compare] attribute compares the value of a property with another property’s value.
Example
public class Account
{
[Compare("ConfirmPassword", ErrorMessage = "Passwords do not match.")]
public string Password { get; set; }
public string ConfirmPassword { get; set; }
}
Display Attribute
The [Display] attribute specifies how a property is displayed in the user interface.
Example
public class Product
{
[Display(Name = "Product Name")]
public string Name { get; set; }
}
Editable Attribute
The [Editable] attribute specifies whether a property should be editable in the user interface.
Example
public class User
{
[Editable(false)]
public string Username { get; set; }
}
ReadOnly Attribute
The [ReadOnly] attribute specifies that a property should be read-only in the user interface.
Example
public class Customer
{
[ReadOnly(true)]
public int Id { get; set; }
}
MaxLength Attribute
The [MaxLength] attribute specifies the maximum length of a string property.
Example
public class Note
{
[MaxLength(100, ErrorMessage = "Note cannot exceed 100 characters.")]
public string Content { get; set; }
}
MinLength Attribute
The [MinLength] attribute specifies the minimum length of a string property.
Example
public class User
{
[MinLength(6, ErrorMessage = "Password must be at least 6 characters long.")]
public string Password { get; set; }
}
RegularExpression Attribute
The [RegularExpression] attribute can also be used for validating other formats, such as postal codes.
Example
public class Address
{
[RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid ZIP code format.")]
public string ZipCode { get; set; }
}
Custom ErrorMessage Attribute
All the examples above can include custom error messages by setting the ErrorMessage property.
Conclusion
Data annotations in .NET offer a powerful toolset for defining validation rules and presentation attributes for model classes. By leveraging attributes like [Required], [StringLength], [Range], and others, developers can enhance data integrity and user interaction in their applications. Understanding and effectively utilizing these annotations is essential for building robust and user-friendly .NET applications.
