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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:25:51+00:00 2026-06-05T07:25:51+00:00

Has anyone seen an MVC3 data annotation for Date validation that requires a single

  • 0

Has anyone seen an MVC3 data annotation for Date validation that requires a single selected date to be equal to or greater than current date?

If there’s already a third party add on that’s cool too. I’m already using the DataAnnotationsExtensions but it doesn’t offer what I’m looking for.

There doesn’t seem to be any reference of this on. So, hoping someone has already solved this before I try to reinvent the wheel and write my own custom validator.

I’ve tried Range but that requires 2 dates and both have to be constants in string format such as [Range(typeof(DateTime), "1/1/2011", "1/1/2016")] but that doesn’t help. And the DataAnnotationsExtensions Min validator only accepts int and double


Update Solved

Thanks to @BuildStarted this is what I ended up with and it works great server-side and now client side with my script


using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace Web.Models.Validation {

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class DateMustBeEqualOrGreaterThanCurrentDateValidation : ValidationAttribute, IClientValidatable {

        private const string DefaultErrorMessage = "Date selected {0} must be on or after today";

        public DateMustBeEqualOrGreaterThanCurrentDateValidation()
            : base(DefaultErrorMessage) {
        }

        public override string FormatErrorMessage(string name) {
            return string.Format(DefaultErrorMessage, name);
        }  

        protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
            var dateEntered = (DateTime)value;
            if (dateEntered < DateTime.Today) {
                var message = FormatErrorMessage(dateEntered.ToShortDateString());
                return new ValidationResult(message);
            }
            return null;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) {
            var rule = new ModelClientCustomDateValidationRule(FormatErrorMessage(metadata.DisplayName));
            yield return rule;
       }
    }

    public sealed class ModelClientCustomDateValidationRule : ModelClientValidationRule {

        public ModelClientCustomDateValidationRule(string errorMessage) {
            ErrorMessage = errorMessage;
            ValidationType = "datemustbeequalorgreaterthancurrentdate";
        }
    }
}

And in my model

[Required]
[DateMustBeEqualOrGreaterThanCurrentDate]
public DateTime SomeDate { get; set; }

The client side script

/// <reference path="jquery-1.7.2.js" />

jQuery.validator.addMethod("datemustbeequalorgreaterthancurrentdate", function (value, element, param) {
    var someDate = $("#SomeDate").val();
    var today;
    var currentDate = new Date();
    var year = currentDate.getYear();
    var month = currentDate.getMonth() + 1;  // added +1 because javascript counts month from 0
    var day = currentDate.getDate();
    var hours = currentDate.getHours();
    var minutes = currentDate.getMinutes();
    var seconds = currentDate.getSeconds();

    today = month + '/' + day + '/' + year + '  ' + hours + '.' + minutes + '.' + seconds;

    if (someDate < today) {
        return false;
    }
    return true;
});

jQuery.validator.unobtrusive.adapters.addBool("datemustbeequalorgreaterthancurrentdate");
  • 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-05T07:25:52+00:00Added an answer on June 5, 2026 at 7:25 am

    Create a custom attribute.

    public class CheckDateRangeAttribute: ValidationAttribute {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
            DateTime dt = (DateTime)value;
            if (dt >= DateTime.UtcNow) {
                return ValidationResult.Success;
            }
    
            return new ValidationResult(ErrorMessage ?? "Make sure your date is >= than today");
        }
    
    }
    

    code was written off the cuff so fix any errors 🙂

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

Sidebar

Related Questions

Has anyone seen an open-source library that produces charts similar to Github.com's commit timeline
Has anyone seen a javascript library that will generate a photo gallery similar to
Has anyone seen a RedirectedThreadFrame in a callstack in windbg? That is from the
Has anyone seen this type of IE display problem? Example http://xs133.xs.to/xs133/08465/ie_problem910.jpg.xs.jpg Note that it
Has anyone seen a snippet (or class/library) that will draw that corner triangle in
Has anyone seen a icon library that has the thin style gray scale look
Has anyone seen an allocator that calls mlock(2) to prevent an STL container's contents
Has anyone seen this error when working with a PHP application out of dynamic
Has anyone seen JavaMail not sending proper MimeMessages to an SMTP server, depending on
Has anyone seen rendering glitches with Papervision? Please seee the flash movie at http://www.mapopolis.com/test/

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.