1

With Asp.Net MVC 4.5, I have a model with a DateTime? property. I have used the javascript datepicker, with the current culture, to set the date. It all seems to be working as far as setting the date. The problem comes during validation. If the culture is one in which the format is dd/mm/yyyy, and the day is > 12, I get a validation error, client side. The strange thing is, I can't figure out where the validation is coming from. I am using fluent validation. The definition of the field is

//[DataType(DataType.Date)]
[Display(Name = "Birthday", ResourceType = typeof(Resource1))]
public DateTime? Birthday { get; set; }

(I removed the DataType in case that's where the validation was coming from). My fluentvalidation rules have no rule for the Birthday field. So, who is generating the client-side validation, and how can I suppress it (or better yet, make sure it's validated correctly for the current culture)?

Edit: I'm using the jqueryui datepicker https://jqueryui.com/datepicker/

Pete
  • 51
  • 1
  • 7
  • How are you creating the edit field in the view - what does the final markup for the field look like in your browser? Is it ``? If so it's possibly coming from the browser. – Zhaph - Ben Duguid Nov 09 '17 at 18:06
  • 1
    Which datepicker widget are you using? Does it do validation? You can also right-click the input and examine the attached events through the browser's dev tools. – Jasen Nov 09 '17 at 18:08
  • The final markup is `` – Pete Nov 09 '17 at 19:53
  • @Jasen Well that's a useful thing to know. There are a lot of events attached, all from code.jquery.com/.... It would surprise me however that the widget itself, given that it is using the correct culture and generating the datetime string properly, would get the validation wrong. – Pete Nov 09 '17 at 19:58
  • @StephenMuecke I don't see how the answer to that question solves my problem, it simply forces everyone to use the same date format. – Pete Nov 10 '17 at 04:49
  • What?? You have saidf that your editing the date in `dd/MM/yyyy` format. Read the duplicate again - carefully this time –  Nov 10 '17 at 04:51
  • 1
    The `$.validator` validates dates in `MM/dd/yyyy` format by default. If you enter `13/1/2017` it assumes you have entered 13 for the month (i.e. a non-valid date). You need to reconfigure the validator to validate dates in `dd/MM/yyyy` format –  Nov 10 '17 at 04:53
  • Doesn't really help that `type="datetime"` is [now obsolete and browser support is on the way out](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime). `type="date"` would be a better option, but would require a bit more work to generate, and most browsers should give you an OotB date picker and no forced validation. – Zhaph - Ben Duguid Nov 10 '17 at 11:21
  • @StephenMuecke Perhaps I didn't explain clearly. I'm using the localized datepicker, so an Italian will get an italian datepicker, and Gennaio 13 will come out as 13/1/2017. An American will get an american datepicker, and January 13 will come out as 1/13/2017. This is the way I want it, each person sees the format he/she is used to. But the validator doesn't seem to be culture-sensitive. My workaround now is to turn off client-side validation for that field, and everything seems to work out ok. Server-side the correct DateTime is being created. – Pete Nov 10 '17 at 17:44

0 Answers0