Click here to Skip to main content
14,177,305 members
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 20 Jul 2010
Licenced CPOL

Building an ASP.NET Validator Using Data Annotations

, 27 Jul 2010
Rate this:
Please Sign up or sign in to vote.
The article describes an implementation of an ASP.NET validator that uses the new System.ComponentModel.DataAnnotations assembly that came in .NET 3.5 SP1.


In this article I’m going to describe an implementation of ASP.NET validator that uses the new System.ComponentModel.DataAnnotations assembly that came in .NET 3.5 SP1.

The Validator

In order to create the validator, you need to inherit from the BaseValidator abstract class and implement its EvaluateIsValid method. I’ve created an ASP.NET Server Control project and added a control class that does the exact thing. You need to reference the System.ComponentModel.DataAnnotations assembly in order for it to work. Here is how it’s implemented:

namespace Validators
  public class DataAnnotationValidator : BaseValidator
    #region Properties

    /// <summary>
    /// The type of the source to check
    /// </summary>
    public string SourceTypeName { get; set; }

    /// <summary>
    /// The property that is annotated
    /// </summary>
    public string PropertyName { get; set; }


    #region Methods

    protected override bool EvaluateIsValid()
      // get the type that we are going to validate
      Type source = GetValidatedType();

      // get the property to validate
      PropertyInfo property = GetValidatedProperty(source);

      // get the control validation value
      string value = GetControlValidationValue(ControlToValidate);

      foreach (var attribute in property.GetCustomAttributes(
               typeof(ValidationAttribute), true)
        if (!attribute.IsValid(value))
          ErrorMessage = attribute.ErrorMessage;
          return false;
      return true;

    private Type GetValidatedType()
      if (string.IsNullOrEmpty(SourceTypeName))
        throw new InvalidOperationException(
          "Null SourceTypeName can't be validated");
      Type validatedType = Type.GetType(SourceTypeName);
      if (validatedType == null)
        throw new InvalidOperationException(
              "Invalid SourceTypeName", SourceTypeName));

      return validatedType;

    private PropertyInfo GetValidatedProperty(Type source)
      PropertyInfo property = source.GetProperty(PropertyName, 
        BindingFlags.Public | BindingFlags.Instance);

      if (property == null)
        throw new InvalidOperationException(
            "Validated Property Does Not Exists", PropertyName));
      return property;


As you can see, I use two main properties in the validator which help me to get the relevant source type name and the property to validate.

Testing the Validator

In order to test the validator, I have created a Web Application project and put inside of it a simple Person class which uses Data Annotations for validation:

namespace ASPNETDataAnnotation 
  public class Person 
    [Required(ErrorMessage="ID is requiered")] 
    public string ID { get; set; } 

    [StringLength(20, ErrorMessage = "First name is too long!")]
    public string FirstName { get; set; } 

    [StringLength(20, ErrorMessage="Last name is too long!")] 
    public string LastName { get; set; } 

      ErrorMessage="Must be a valid e-mail address")] 
    public string Email { get; set; } 

Also, I’ve created a web form that uses the validator:

lt;%@ Page Language="C#" AutoEventWireup="true" 
  CodeBehind="WebForm1.aspx.cs" Inherits="ASPNETDataAnnotation.WebForm1" %>

<%@ Register Assembly="Validators" Namespace="Validators" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
            <asp:Label ID="lblID" Text="ID: " runat="server" />
            <asp:TextBox ID="txtID" runat="server" />
            <cc1:DataAnnotationValidator ID="valID" 

                runat="server" ControlToValidate="txtID"


                SourceTypeName="ASPNETDataAnnotation.Person, ASPNETDataAnnotation" />
            <asp:Label ID="lblFirstName" Text="First Name: " runat="server" />
            <asp:TextBox ID="txtFirstName" runat="server" />
            <cc1:DataAnnotationValidator ID="valFirstName" 

                runat="server" ControlToValidate="txtFirstName"


                SourceTypeName="ASPNETDataAnnotation.Person, ASPNETDataAnnotation" />
            <asp:Label ID="lblLastName" Text="Last Name: " runat="server" />
            <asp:TextBox ID="txtLastName" runat="server" />
            <cc1:DataAnnotationValidator ID="valLastName" 

                runat="server" ControlToValidate="txtLastName"


                SourceTypeName="ASPNETDataAnnotation.Person, ASPNETDataAnnotation" />
            <asp:Label ID="lblEmail" Text="E-Mail: " runat="server" />
            <asp:TextBox ID="txtEmail" runat="server" />
            <cc1:DataAnnotationValidator ID="valEmail" 

                runat="server" ControlToValidate="txtEmail"


                SourceTypeName="ASPNETDataAnnotation.Person, ASPNETDataAnnotation" />
            <asp:Button ID="btnValidate" Text="Validate" runat="server" />

Running the web form and clicking on the button while we have bad data will generate a validation error as expected:

WebForm Validation Results


In the post, I showed one way to create an ASP.NET data annotation validator. In other frameworks like ASP.NET MVC or WCF RIA Services you have such validators implemented.


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


About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website:

You may also be interested in...


Comments and Discussions

GeneralComparing with enterprise library 5.0 Pin
Mogamboo_Khush_Hua19-Sep-10 21:59
memberMogamboo_Khush_Hua19-Sep-10 21:59 
GeneralRe: Comparing with enterprise library 5.0 Pin
Mogamboo_Khush_Hua19-Sep-10 22:02
memberMogamboo_Khush_Hua19-Sep-10 22:02 
GeneralRe: Comparing with enterprise library 5.0 Pin
Gil Fink19-Sep-10 22:35
memberGil Fink19-Sep-10 22:35 
Validation using ent-lib isn't the same like data annotations (even though it seems like they do the same thing).
The purpose of data annotation is to add metadata to properties to indicate simple validation scenarios. Putting the annotations won't add validation to your application and you will have to supply your implementation or the framework that you use (for example MVC framework) will supply built in validation implementation for you.
The Validation Application Block is a framework that was built to give solution to variaty of validation scenarios. It includes the validation implementation itself and also include more features that data annotations can't supply like self validation for complex business validations for example.

I hope it clarify the subject.
NewsASP.NET Data Annotation Validator with Client Side Support Pin
Gil Fink1-Aug-10 19:28
memberGil Fink1-Aug-10 19:28 
GeneralRe: ASP.NET Data Annotation Validator with Client Side Support Pin
amanek14-Jan-12 1:16
memberamanek14-Jan-12 1:16 
GeneralSmall correction Pin
Richard Deeming27-Jul-10 4:53
mveRichard Deeming27-Jul-10 4:53 
GeneralRe: Small correction Pin
Gil Fink27-Jul-10 20:03
memberGil Fink27-Jul-10 20:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190526.1 | Last Updated 28 Jul 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid