I have a model with a property of type DateTime
:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
[DataType(DataType.Date, ErrorMessage="Data em formato inválido")]
public DateTime? Date {get;set;}
And in View
:
@Html.TextBoxFor(model => model.Date)
@Html.ValidationMessageFor(model => model.Date)
When typing a date with the following value: 11/11/1111, it is the value not the format, and I send the POST, it passes in ModelState
, but in SaveChange
it causes error:
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.
What I have done so far and I did not succeed:
1 - In my Web.config
the following attribute:
<globalization uiCulture="pt-BR" culture="pt-BR" enableClientBasedCulture="true" requestEncoding="UTF-8" responseEncoding="UTF-8" fileEncoding="UTF-8" />
2 - Installation of jQuery
globalize
and jQuery Validate
globalize
.
The load order is this:
<script src="~/Scripts/libs/jquery.globalize/globalize.js"></script>
<script src="~/Scripts/libs/jquery.globalize/cultures/globalize.culture.pt-BR.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Content/Scripts/libs/jquery.validate/jquery.validate.globalize.min.js"></script>
3 - Handling number and dates using jQuery Validate
+ globalize
, with the following code:
jQuery.extend(jQuery.validator.methods, {
date: function (value, element) {
return this.optional(element) || /^\d\d?\/\d\d?\/\d\d\d?\d?$/.test(value);
},
number: function (value, element) {
return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
}
});
How to validate this date entry or create a "range" in the client rather than the server?