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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 521k
  • Answers 520k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer $attrs = $rss->channel->children("atom", true)->link->attributes(); $attrs["href"] = "href_value"; Example here. May 16, 2026 at 8:55 pm
  • Editorial Team
    Editorial Team added an answer To answer my own question:the last option works. The problem… May 16, 2026 at 8:55 pm
  • Editorial Team
    Editorial Team added an answer You can use Option's flatMap and map methods to combine… May 16, 2026 at 8:55 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Is there any way to embed a widget in a data-bound DataGridViewCell when it
I am wanting to create a Custom Document Library for the purpose of targeting
We're wanting to add some properties to our MOSS user profiles and expose them
I was wanting to create a personal note database, to store notes in HTML
I'm moving away from strict Android development and wanting to create iPhone applications. My
I am new to the .Net world, and was wanting to create a class
I have a very simple WPF application in which I am using data binding
Here is the situation: User looks something up. Alert sound is played because there
wget is helpful in my data mining projects. Today I try to wget the
Okay, so I've created a custom template for Joomla, and gotten that working. However,

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.