2

I have a senario in my project where i have to compare dates in order to find whether the start date is greater that end date. So as far MVC3 is not providing there own attribute for comparing dates. so for this purpose i have created my own custom attribute to compare dates which is given below

namespace WebUtil.Validators { using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Web.Mvc; using WebUtil.Resources;

public class CompareDateAttribute : ValidationAttribute, IClientValidatable
{

    public CompareDateAttribute(string otherPropertyName)
        : base(WebUtilResources.Validation_Invalid_Dates)
    {
        this.OtherControlName = otherPropertyName;
    }

    public string OtherControlName { get; set; }

    public override string FormatErrorMessage(string name)
    {
        return string.Format(ErrorMessageString, name, this.OtherControlName);
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(
        ModelMetadata metadata, ControllerContext context)
    {
        var greaterRule = new ModelClientValidationRule();
        greaterRule.ErrorMessage = this.FormatErrorMessage(metadata.GetDisplayName());
        greaterRule.ValidationType = "greater";
        greaterRule.ValidationParameters.Add("other", this.OtherControlName);
        yield return greaterRule;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var otherPropertyInfo = validationContext.ObjectType.GetProperty(this.OtherControlName);
        var otherDate = (DateTime)otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
        var thisDate = (DateTime)value;

        if (thisDate <= otherDate)
        {
            var message = this.FormatErrorMessage(validationContext.DisplayName);
            return new ValidationResult(message);
        }

        return null;
    }
}

}

After creating this attribute in order to do this validation in client side i have added a method to JQuery unobstrusive validator like this.

 jQuery.validator.addMethod("greater", function (value, element, param) {
    return Date.parse(value) > Date.parse($(param).val());
 });

 jQuery.validator.unobtrusive.adapters.add("greater", ["other"], function (options) {
    options.rules["greater"] = "#" + options.params.other;
    options.messages["greater"] = options.message;
 });

 jQuery.validator.addMethod('greaterThanDate', function (value, element, params) {
    var date = value.toString().split('/');
    var PassDay = date[0];
    var PassMonth = date[1];
    var passyear = date[2];
    if (PassDay != null && PassMonth != null && passyear != null) {
        var passDate = new Date(passyear, PassMonth - 1, PassDay);

        var currentdate = new Date();
        return passDate < currentdate;
    }
    return true;
 }, '');

 // and an unobtrusive adapter
 jQuery.validator.unobtrusive.adapters.add('futuredate', {}, function (options) {
    options.rules['greaterThanDate'] = true;
    options.messages['greaterThanDate'] = options.message;
 });

But this validation works fine for me in all browsers except current stable version of Chrome. I am very much confused. Can anybody give me a help in resolving this?

Emre Erkan
  • 8,433
  • 3
  • 48
  • 53
Febin J S
  • 1,358
  • 3
  • 26
  • 53

2 Answers2

1

I have resolved this, this is just a culture specific issue while parsing date in chrome. Iam not exact why the culture issue is coming only in chrome not in other browsers.

Anyway i resolved this by taking the answer of the following post

Jquery Date.parse returning NaN in Chrome browser?

Community
  • 1
  • 1
Febin J S
  • 1,358
  • 3
  • 26
  • 53
1

You might try <%: Html.JQueryDatePickerGlobalizationScript() %> from the MVC Controls Toolkit

Html.JQueryDatePickerGlobalizationScript() sets the culture of the DatePicker to the CurrentCulture of the current Thread, and use the Globalization/Localization scripts in the CDN: http://jquery-ui.googlecode.com/svn/trunk/ui/i18n/

Community
  • 1
  • 1
RickAndMSFT
  • 20,912
  • 8
  • 60
  • 78