0

I know that my question has been discussed on several occasions, but I don't seem to able to find a clear answer to my problem. What am I doing incorrectly?!

I am using Bootstrap datepicker and keep receiving the message "The Field {name} must be a date." I'm sure that the issue is to do with formatting, because when I enter 01/01/2016 it is fine but 13/01/2016 shows the validation error.

I have read that this is to do with the datepicker format, or globalization settings but I think that I've covered most of these; others have referred to problems with partial views but I don't think that this is my issue as the date is not in a partial view. What have I done wrong?

Date Picker code:

<script type="text/javascript">
    $('.datepicker').datepicker({ format: 'dd/mm/yyyy', startView: 'years' }); //Initialise any date pickers

</script>

View:

<div class="form-group">
    @Html.LabelFor(m => m.BirthDate, new { @class = "col-md-2 control-label" })
    <div class="col-md-10">
        @Html.TextBoxFor(m => m.BirthDate, new { @class = "form-control datepicker", @id = "DateOfBirth" }) 
        @Html.ValidationMessageFor(m => m.BirthDate, "", new { @class = "text-danger" })
    </div>
</div>

Model

[Required(ErrorMessage = "Date of Birth is required")]
[Display(Name = "Birth Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime BirthDate { get; set; }

Web Config

  <system.web>
    <globalization uiCulture="en-GB" culture="en-GB" />
  </system.web>
Tieson T.
  • 20,774
  • 6
  • 77
  • 92
Ste
  • 59
  • 2
  • 10
  • Which datepicker (there is more than one Bootstrap-based datepicker), and what browsers are you testing with? – Tieson T. Sep 18 '16 at 08:53
  • Even though the dupe is for the jquery-ui datepicker, its the same issue - `jquery.validate.js` validates dates based on `MM/dd/yyyy` format by default, so you must reconfigure the validator (note `[DataType(DataType.Date)]` is not required if your using a jquery datepicker) –  Sep 18 '16 at 09:01
  • And you might want to consider going through the answers to your previous questions and accepting those that clearly worked for you! –  Sep 18 '16 at 09:10
  • Oops! Apologies, my misunderstanding of how StackOverflow works - I thought that I had to vote but it was't letting me! Will update ASAP. – Ste Sep 19 '16 at 15:14

0 Answers0