Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 976665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:47:00+00:00 2026-05-16T03:47:00+00:00

Wanting to create custom data annotation validation. Are there any useful guides / samples

  • 0

Wanting to create custom data annotation validation. Are there any useful guides / samples on how to create them?

Firstly:
StringLength with minimum and maximum length. I’m aware .NET 4 can do this, but want to do the same in .NET 3.5, if possible being able to define minimum length only (at least x chars), maximum length only (up to x chars), or both (between x and y chars).

Secondly:
Validation using modulus arithmetic – if the number is a valid length, I wish to validate using the Modulus 11 algorithm (I have already implemented it in JavaScript, so I guess it would just be a simple porting?)

Update:
Solved second problem, was just a case of copying over the JavaScript implementation and making a few tweaks, so don’t need a solution for that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:47:01+00:00Added an answer on May 16, 2026 at 3:47 am

    To create a custom data annotation validator follow these gudelines:

    1. Your class has to inherit from System.ComponentModel.DataAnnotations.ValidationAttribute class.
    2. Override bool IsValid(object value) method and implement validation logic inside it.

    That’s it.

    IMPORTANT Caution

    Sometimes developers check that value is not null/empty and return false. This is usually incorrect behaviour, because that’s on Required validator to check which means that your custom validators should only validate non-null data but return true otherwise (see example). This will make them usable on mandatory (required) and non-mandatory fields.

    Example

    public class StringLengthRangeAttribute : ValidationAttribute
    {
        public int Minimum { get; set; }
        public int Maximum { get; set; }
    
        public StringLengthRangeAttribute()
        {
            this.Minimum = 0;
            this.Maximum = int.MaxValue;
        }
    
        public override bool IsValid(object value)
        {
            string strValue = value as string;
            if (!string.IsNullOrEmpty(strValue))
            {
                int len = strValue.Length;
                return len >= this.Minimum && len <= this.Maximum;
            }
            return true;
        }
    }
    

    All properties can be set in attribute as you wish to set them.
    Some examples:

    [Required]
    [StringLengthRange(Minimum = 10, ErrorMessage = "Must be >10 characters.")]
    
    [StringLengthRange(Maximum = 20)]
    
    [Required]
    [StringLengthRange(Minimum = 10, Maximum = 20)]
    

    When a particular property isn’t set, its value is set in the constructor, so it always has a value. In above usage examples I deliberately added the Required validator as well, so it’s in sync with the above caution I’ve written.

    Important

    So this validator will still work on your model value that’s not required, but when it’s present it validates (think of a text field in a web form, that’s not required, but if a user enters a value in, it has to be valid).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wanting to create a program that talks with a Cometd server to
This situation arises from someone wanting to create their own pages in their web
Not wanting to re-invent the wheel or anything, I was wondering if there's a
Is there a built in method for waiting for a file to be created
Wanting to implement authentication by client certificates I am experiencing some issues. First some
I wanting to show prices for my products in my online store. I'm currently
I'm wanting to parse a string into a nullable int in C#. ie. I
I'm wanting extra security for a particular point in my web app. So I
I am wanting to access a website from a different port than 80 or
I'm wanting to get the full value of a char[] variable in the VC6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.