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 7982641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:43:52+00:00 2026-06-04T10:43:52+00:00

I have a custom ValidationAttribute in my ASP.NET MVC3 project which has two conditions

  • 0

I have a custom ValidationAttribute in my ASP.NET MVC3 project which has two conditions which need to be met. It works well but I would like to let the user now which validation rule has been broken by returning a customised error message.

With the method I am using (inherit error message from base class) I know I can not change the value of the _defaultError constant after it has been initialised, so….

How do I return different error messages depending on which condition was not met?

Here is my ValidationAttribute code:

public class DateValidationAttribute :ValidationAttribute
{
    public DateValidationAttribute() 
        : base(_defaultError)
    {

    }

    private const string _defaultError = "{0} [here is my generic error message]";

    public override bool IsValid(object value)
    {
        DateTime val = (DateTime)value;

        if (val > Convert.ToDateTime("13:30:00 PM"))
        {
            //This is where I'd like to set the error message
            //_defaultError = "{0} can not be after 1:30pm";
            return false;
        }
        else if (DateTime.Now.AddHours(1).Ticks > val.Ticks)
        {
            //This is where I'd like to set the error message
            //_defaultError = "{0} must be at least 1 hour from now";
           return false;
        }
        else
        {
            return true;
        }

    }
}
  • 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-06-04T10:43:54+00:00Added an answer on June 4, 2026 at 10:43 am

    I could advise you to create two different implementation of DateValidator class, each having different message. This is also in line with SRP as you just keep the relevant validation information in each validator separate.

    public class AfternoonDateValidationAttribute : ValidationAttribute
    {
       // Your validation logic and message here
    }
    
    public class TimeValidationAttribute : ValidationAttribute
    {
       // Your validation logic and message here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC 2 project in which I've created a data transfer
I have made custom attribute in my asp.net mvc2 project: [AttributeUsage(AttributeTargets.Property, AllowMultiple = false,
I've been fiddling with some ASP.NET MVC3 solution structures and I have settled on
I have a custom asp.net mvc class validation attribute. My question is how can
In my Asp.net MVC app, I have a custom validator class V and an
I have custom type name Netsgroup which is a collection of Net class. Net
I have a custom validation attribute which checks to see if two properties have
I have custom event that has several different subscribers who will all use the
I have custom UITableViewCell s as well as headers and footers. Each of them
I have custom map image with specific height and width. i need to map

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.