C# custom validation ValidationAttribute

Using the ValidationAttribute we can handle custom validation. It is available in namespace: System.ComponentModel.DataAnnotations Below is snippet for the same here age is being evaluated.

public class AgeValidator : ValidationAttribute
{
  public int Age { get; set; }

  public override bool IsValid(object value)
  {
    DateTime dob;
    if (value != null
       && DateTime.TryParse(value.ToString(), out dob))
    {
      if (dob.AddYears(Age) <= DateTime.Now)
      {
        return true;
      }
    }
    return false;
  }
}

12 thoughts on “C# custom validation ValidationAttribute”

  1. Whats up this is kind of of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding know-how so I wanted to get advice from someone with experience. Any help would be greatly appreciated!

  2. Great post. I was checking constantly this blog and I’m impressed! Extremely useful info particularly the last part 🙂 I care for such info much. I was seeking this certain info for a long time. Thank you and best of luck.

  3. I simply want to say I am all new to blogs and certainly enjoyed you’re website. Almost certainly I’m planning to bookmark your website . You amazingly come with beneficial article content. Many thanks for sharing with us your website.

  4. Thanks for your concepts. One thing we have noticed is that often banks and financial institutions understand the spending behaviors of consumers plus understand that many people max away their own credit cards around the breaks. They wisely take advantage of this particular fact and commence flooding your current inbox in addition to snail-mail box by using hundreds of no-interest APR card offers shortly after the holiday season concludes. Knowing that should you be like 98 of all American community, you’ll leap at the possible opportunity to consolidate consumer credit card debt and transfer balances to 0 rate credit cards.

  5. I am curious to find out what blog system you’re working with?
    I’m experiencing some small security problems with my latest blog and I would like to find
    something more risk-free. Do you have any recommendations?

  6. My programmer is trying to convince me to
    move to .net from PHP. I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using Movable-type on numerous
    websites for about a year and am anxious about switching to another platform.
    I have heard fantastic things about blogengine.net. Is there a way I can import all my wordpress content into it?
    Any kind of help would be really appreciated!

  7. Helpful info. Fortunate me I found your site accidentally,
    and I am surprised why this twist of fate didn’t came about in advance!

    I bookmarked it.

Leave a Reply

Your email address will not be published. Required fields are marked *