0

I have added the unobtrusive.. etc all works except if i have GreaterThan on my second date field

StartDate: 2017/02/25 12:00 AM End Date: 2017/02/25 01:03 PM

it seems to think that the End Date is not greater, i assume its doing a string comparison vs a proper date because that is a valid range as the end date is greater than the start but if we are doing a string compare of that then the end date is not greater

here is my code

    [Required]
    [DataType(DataType.DateTime)]
    [Display(ResourceType = typeof(DisplayResources), Name = "StartDateTime")]
    public DateTime StartDateTime { get; set; }

    [Required]
    [DataType(DataType.DateTime)]
    [Display(ResourceType = typeof(DisplayResources), Name = "EndDateTime")]
    [GreaterThan("StartDateTime")]
    public DateTime EndDateTime { get; set; }

`

update this is the component i am using https://eonasdan.github.io/bootstrap-datetimepicker/ which uses moment.js for its date formats

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
Zoinky
  • 4,083
  • 11
  • 40
  • 78
  • I suspect the problem here is that your dates are not in a valid format for `jquery.validate.js` client side validation which uses `MM/dd/yyyy` or ISO format - try entering `2017-02-25 12:00 AM` and `2017-02-25 01:03 PM` respectively (which will be valid) and then say `2016-02-25 01:03 PM` for the end date which will give a client side error message –  Feb 26 '17 at 01:06
  • the component i am using uses https://momentjs.com/ momentjs for its dating format, and i cant seem to find the right one that will work with jquery dates – Zoinky Feb 26 '17 at 01:27
  • Have just checked the foolproof script and even the format I noted above will not work. The problem is the `var isDate = function (input) { ..` function in the `mvcfoolproof.unobtrusive.js` file - the regex fails and therefore makes a comparison based on a `string`,not a `date` –  Feb 26 '17 at 01:31
  • Your will need to use a format that passes the regex - e.g. `02/25/2017 12:00 AM` and `02/25/2017 01:03 PM` –  Feb 26 '17 at 01:33
  • momentjs doesnt seem to support format: "MM/DD/YYYY hh:mm a" – Zoinky Feb 26 '17 at 01:37
  • According to the docs it does. What does your script for the plugin look like? –  Feb 26 '17 at 03:49
  • it seems that works but puts default date of 01/01/0001 12:00 am in the field, i had to set a minDate as well in order for the field to be blank, so far it seems to work because the control doesnt allow the wrong ranges to be picked (start and end dates are linked) – Zoinky Feb 26 '17 at 14:08
  • I have not used that plugin, but if you want a blank value initially, then you should make the properties nullable - `public DateTime? EndDateTime { get; set; }` –  Feb 26 '17 at 21:01

1 Answers1

0

I think this works for you.

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (StartDateTime> EndDateTime)
       {
            yield return
         new ValidationResult(errorMessage: "End date must be greater than Start date",
                             memberNames: new[] { "EndDateTime" });
        }

    }