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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:24:46+00:00 2026-05-24T16:24:46+00:00

I have 3 properties for a custom class, its Date, Hour and Minute. These

  • 0

I have 3 properties for a custom class, its Date, Hour and Minute. These represent fields the user can enter, but they are not required, but if one field is filled in then the other 2 should as well (Not sure that part is possible with pure c#)

I have made this code

    [RegularExpression("[0-9]{2}-[0-9]{2}-[0-9]{4}", ErrorMessage = "Date should be in the following format: dd-mm-yyyy")]
    [MaxLength(10)]
    public string Date { get; set; }

    [CustomRange(0, 24, ErrorMessage = "Hour must be between 00 and 24")]
    public string Hour { get; set; }

    [CustomRange(0, 59, ErrorMessage = "Hour must be between 00 and 59")]
    public string Minute { get; set; }

.

    public class CustomRangeAttribute : ValidationAttribute
{
    public int Min { get; set; }
    public int Max { get; set; }


    public CustomRangeAttribute(int min, int max)
    {
        Min = min;
        Max = max;
    }

    public override bool IsValid(object value)
    {
        var stringValue = Convert.ToString(value, CultureInfo.CurrentCulture);
        int tmp;
        if(int.TryParse(stringValue, out tmp))
        {
            return tmp >= Min && tmp <= Max;
        }
        return false;
    }
}

The date part works fine, that one is optional, but the hour and minute it complains about as if they had [Required], so is that possible to make them optional as well?

I changed so I used regular expressions instead

    [RegularExpression("[0-9]{2}-[0-9]{2}-[0-9]{4}", ErrorMessage = "Date should be in the following format: dd-mm-yyyy")]
    [MaxLength(10)]
    public string Date { get; set; }

    [RegularExpression("[0-1]{1}[0-9]{1}|[2]{1}[0-3]{1}")]
    public string Hour { get; set; }

    [RegularExpression("[0-5]{1}[0-9]{1}")]
    public string Minute { get; set; }
  • 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-24T16:24:46+00:00Added an answer on May 24, 2026 at 4:24 pm

    To meet your requires of certain fields being required only in some situations, you can implement custom validation logic using IValidateableObject

    How do I use IValidatableObject?

    Provides a good overview

    basically

    public MyCustomClass:IValidateableObject
    {
    
     public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)    
     { 
          //your custom "model-wide" business rules here
     }
    }
    

    As for the Hour and Minute, is there any reason your using a string instead of a nullable int?

    ie

    [Range(0,24,ErrorMessage="Hour must be between 00 and 24")]
    public int? Hour {get; set;}
    

    Edit: Ok now that you have edited your first post and included your Custom Validation Attribute its obvious why it isn’t working

    Your current code

     public override bool IsValid(object value)     { 
            var stringValue = Convert.ToString(value, CultureInfo.CurrentCulture); 
            int tmp;
             if(int.TryParse(stringValue, out tmp))
             {
                 return tmp >= Min && tmp <= Max;
             } 
            return false; 
        }
    

    Will return false whenever value == null.

    Adding a check for a nullorempty value is what you need ie

       public override bool IsValid(object value)     { 
           var stringValue = Convert.ToString(value, CultureInfo.CurrentCulture); 
    
           if (String.IsNullOrEmpty(stringValue))
           {
                return true;
           }
            int tmp;
             if(int.TryParse(stringValue, out tmp))
             {
                 return tmp >= Min && tmp <= Max;
             } 
            return false; 
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of custom objects. These objects have 2 datetime properties on
I have created a custom C# (4.0) class to be used in properties of
So, I have a list containing a custom class, MyClass MyClass has properties, which
I have a custom control which inherits from System.Web.UI.Control and some of its properties
I have a Silverlight custom control with two properties; Text and Id. I have
Using NH 2.0, I have a custom type. It is composed of four properties,
Regular Expressions are usually expressed as strings, but they also have properties (ie. single
I have an NSMutableArray of custom objects. The custom object (Tag Class) has an
I have a base class for some kind of user controls, and in that
I have a custom asp-net control that inherits from another one and its works

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.