I need to override the validator function for a date field defined as:
@*Creation*@
<div class="form-group">
@Html.LabelFor(model => model.Creation, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.Creation, "{0:dd/MM/yyyy}", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Creation)
</div>
</div>
Accordingly to this thread, this should be a simple solution to fix my date format issue, but when I try to do the suggested approach, no validation happens at all.
Due to my lack of experience in ASP MVC and jquery, I might be missing something simple. I added this override on the same view (cshtml) of the form, under the Scripts Section:
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
jQuery(function ($) {
$.validator.addMethod('date',
function (value, element) {
$.culture = Globalize.culture("pt-BR");
var date = Globalize.parseDate(value, "dd/MM/yyyy", "pt-BR");
return this.optional(element) ||
!/Invalid|NaN/.test(new Date(date).toString());
});
});
</script>
});
No validation happens when the script is there. If I remove the script, the validation does not respect the date format.
How can I fix that?