I have made an MVC application where the user selects a start date and finish date. They input this data using a jQuery datepicker. However when I click save I get a validation message saying that it's not a validate date. The textbox
accepts the date format yyyy/mm/dd. Not sure why I'm getting these validation messages as I have tried a number of things.
My code is as follows
Create.cshtml View
<div class="form-group">
<script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>
<link href="@Url.Content("/Content/jquery-ui.min.css")" rel="stylesheet" type="text/css" />
@Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="text" id="MyDate"
@Html.EditorFor(model => model.StartDate, "{0:dd/MM/yyyy}", new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" }) />
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FinishDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="text" id="FinishDate"
@Html.EditorFor(model => model.FinishDate, "{0:dd/MM/yyyy}", new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FinishDate, "", new { @class = "text-danger" }) />
</div>
</div>
<p>
<button class="btn btn-success"><span class="glyphicon glyphicon-plus"></span>Create</button>
</p>
</div>
}
<script type="text/javascript">
$(document).ready(function () {
$("#MyDate").datepicker();
$.validator.methods.date = function (value, element) {
Globalize.culture("en-AU");
// you can alternatively pass the culture to parseDate instead of
// setting the culture above, like so:
// parseDate(value, null, "en-AU")
return this.optional(element) || Globalize.parseDate(value) !== null;
}
});
</script>
<script type="text/javascript">
$(document).ready(function () {
$("#FinishDate").datepicker();
});
</script>
<div>
<a class=" btn btn-primary" href="@Url.Action("Index")"><span class=" glyphicon glyphicon-home"></span> Task List</a>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
I also tried adding this to the webconfig
<globalization culture="en-AU" uiCulture="en-AU" />