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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:08:20+00:00 2026-05-31T09:08:20+00:00

i wnat to validate the datetime, My Code is: [Range(typeof(DateTime), DateTime.Now.AddYears(-65).ToShortDateString(), DateTime.Now.AddYears(-18).ToShortDateString(), ErrorMessage =

  • 0

i wnat to validate the datetime, My Code is:

    [Range(typeof(DateTime), 
     DateTime.Now.AddYears(-65).ToShortDateString(), 
     DateTime.Now.AddYears(-18).ToShortDateString(),
     ErrorMessage = "Value for {0} must be between {1} and {2}")]
    public DateTime Birthday { get; set; }

but i get the error:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

please help me?

  • 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-31T09:08:21+00:00Added an answer on May 31, 2026 at 9:08 am

    This means the values for the Range attribute can’t be determined at some later time, it has to be determined at compile time. DateTime.Now isn’t a constant, it changes depending on when the code runs.

    What you want is a custom DataAnnotation validator. Here’s an example of how to build one:

    How to create Custom Data Annotation Validators

    Put your date validation logic in IsValid()

    Here’s an implementation. I also am using DateTime.Subtract() as opposed to negative years.

    public class DateRangeAttribute : ValidationAttribute
    {
        public int FirstDateYears { get; set; }
        public int SecondDateYears { get; set; }
    
        public DateRangeAttribute()
        {
            FirstDateYears = 65;
            SecondDateYears = 18;
        }
    
        public override bool IsValid(object value)
        {
            DateTime date = DateTime.Parse(value); // assuming it's in a parsable string format
    
            if (date >= DateTime.Now.AddYears(-FirstDateYears)) && date <= DateTime.Now.AddYears(-SecondDateYears)))
            {
                return true;
            }
    
            return false;
    }
    

    }

    Usage is:

    [DateRange(ErrorMessage = "Must be between 18 and 65 years ago")]
    public DateTime Birthday { get; set; }
    

    It’s also generic so you can specify new range values for the years.

    [DateRange(FirstDateYears = 20, SecondDateYears = 10, ErrorMessage = "Must be between 10 and 20 years ago")]
    public DateTime Birthday { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using the code down below to loop out 200 buttons and wnat the row
in my app I have 49 points with x,y and I wnat to display
I have a datatable with 17 columns and a bunch of data. I wnat
I had code to find label in gridview and I checked on it,s label
i have populated gridview from SaleDAL class, assuming following is the code, Please ignore
The place were I wnat to use the YUI DataTable is in a wiki
I want to use TabBar and wnat to also have more than one views.
I want to bold specific letters in my EditField. public void changeToBold() { try
I wnat all the records in table belonging to a set and where a
first things first; I am writing a little LUA-Ide in C#. The code execution

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.