27

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");
Simon Hughes
  • 3,534
  • 3
  • 24
  • 45
CD Smith
  • 6,597
  • 7
  • 40
  • 66
  • Server DateTime.Today and client side datetime could be different. I think you should be passing a server side time instead of using JavaScript new Date(). – Maksym Kozlenko Dec 10 '12 at 04:22
  • Another issue is that you are assuming that client has US date settings (mm/dd/yyyy) (today = month + '/' + day + '/' + year + ' ' + hours + '.' + minutes + '.' + seconds;) It will not work in other locales. – Maksym Kozlenko Dec 10 '12 at 04:27
  • Good call, but we don't support any other locale than US – CD Smith Dec 10 '12 at 12:59
  • 3
    Too bad. That means I won't be able to use your website :-( I've got en-AU locale on my desktop and ru-UA locale on my laptop. – Maksym Kozlenko Dec 10 '12 at 21:55

3 Answers3

23

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 :)

reggaemahn
  • 6,272
  • 6
  • 34
  • 59
Buildstarted
  • 26,529
  • 10
  • 84
  • 95
13

Use [Remote] for special validations, simple and easy:

Your model:

[Remote("ValidateDateEqualOrGreater", HttpMethod="Post", 
    ErrorMessage = "Date isn't equal or greater than current date.")]
public DateTime Date { get; set; }
//other properties

Your action:

[HttpPost]
public ActionResult ValidateDateEqualOrGreater(DateTime Date)
{
     // validate your date here and return True if validated
     if(Date >= DateTime.Now)
     {
       return Json(true);
     }
     return Json(false);    
}
Community
  • 1
  • 1
Matija Grcic
  • 12,963
  • 6
  • 62
  • 90
  • I considered that but that's not the direction I want to go, I'll need to create the client side jquery for it as well. Good suggestion tho! – CD Smith Jun 04 '12 at 20:40
  • No client side jquery is needed. – Matija Grcic Jun 04 '12 at 20:47
  • Not for the remote, I meant for the data annotation that I would prefer to use. I don't want a separate call to the server for it. – CD Smith Jun 04 '12 at 20:49
  • Why not, it's fast and simple. No need to make a custom attribute and register it in the global.asax. I use it all the time for specific validations. – Matija Grcic Jun 04 '12 at 20:51
  • Partly because I'm already using this strategy for all of my other custom validation, so I'm already registering them so the chosen pattern has now become our "required" pattern. So I don't want to get into mixing strategies when I already have one in place. It's a good suggestion tho, I'm not knocking it :-) Just not what I'm looking for – CD Smith Jun 04 '12 at 20:55
-5

Simple way to accomplish this task is using CompareValidator.

The code below uses AjaxControlToolKit's CalendarExtender. Copy the below code to your HTML directive

<asp:TextBox ID="txtCompletionDate" runat="server" CssClass="txtNormal"></asp:TextBox>
                        <cc1:CalendarExtender ID="CalendarExtender1" TargetControlID="txtCompletionDate"
                            Format="dd/MM/yyyy" runat="server">
                        </cc1:CalendarExtender>
                        <asp:RequiredFieldValidator ID="rfvCompletionDate" runat="server" ControlToValidate="txtCompletionDate"
                            CssClass="labelError" ErrorMessage="*"></asp:RequiredFieldValidator>
                        <asp:CompareValidator ID="cvDate" runat="server" ControlToCompare="hiddenbox" ErrorMessage="*"
                            ControlToValidate="txtCompletionDate" CssClass="labelError" ToolTip="Completion  Date should be greater than or equal to Today"
                            Operator="GreaterThanEqual" Type="Date"></asp:CompareValidator>                        
                        <asp:TextBox ID="hiddenbox" runat="server" CssClass="hiddenbox">
</asp:TextBox>

add the below line in the CSS

.hiddenbox {display:none;}
Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
stanly
  • 19