Click here to Skip to main content
12,691,702 members (32,282 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked

Using Data Annotations in the .NET Framework

, 1 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use data annotations in the .NET Framework

Starting with .NET 4 or MVC3, a developer could use a data annotation on a property to force data validation. This is extremely powerful especially for MVC developers. The same data annotations can also be used when building custom modules for Orchard CMS.

The annotations built into the framework include the following:

  • Required – Allows you to mark a property as being required
  • StringLength – Allows a maximum string length to be specified for a property
  • Range – Validates the value of the specified property is between a range of values
  • RegularExpression – Allows you to specify a regular expression to validate the content against. A comprehensive list of regular expressions can be found at

In addition to these above, custom annotations can be built by inheriting from the base class ValidationAttribute.

An example of a custom data annotation is shown below. This sample asks for a start and an end date to be specified as strings. The assumption is that these strings will be in a correct date format. A property value will be specified as a string. The property value must be between the two dates specified.

public class DateRange : System.ComponentModel.DataAnnotations.ValidationAttribute
    public string StartDate { get; set; }
    public string EndDate { get; set; }

    public DateRange() {
        this.StartDate = new DateTime(1900, 1, 1).ToString();
        this.EndDate = new DateTime(2099, 1, 1).ToString();

    public override bool IsValid(object value) {
        var valueToString = value as string;
        if (!string.IsNullOrEmpty(valueToString)) {
            DateTime dateTimeResult;
            if (DateTime.TryParse(valueToString, out dateTimeResult)) {
                return ((dateTimeResult >= DateTime.Parse(this.StartDate)) && 
                        (dateTimeResult <= DateTime.Parse(this.EndDate)));

            return false;
        return true;

Notice that in the code snippet above, I’m allowing null or empty strings to be passed in. Keep in mind that the Required annotation already checks for nulls or empty strings.

To use the DateRange annotation, simply add it to a property in the model like below:

[DateRange(StartDate="1/1/2012", EndDate="12/31/2012", 
ErrorMessage="The date must be during the 2012 calendar year.")]
public string EventDate { get; set; }

Additional data annotations can be downloaded from a project started by Scott Kirkland at


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jason N. Gaylord
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170117.1 | Last Updated 1 Oct 2012
Article Copyright 2012 by Jason N. Gaylord
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid