I am working on an asp.net mvc-4 web application and i am using jQuery 1.11.2. and i have a field of type DateTime, where i defined a jQuery datepicker to populate its value as follow:-
@Html.DisplayNameFor(model => model.LiveDate) </span>
@Html.TextBoxFor(model => model.LiveDate, new { @class = "datepicker", @Value = (Model != null && Model.LiveDate.HasValue) ? Model.LiveDate.Value.ToString("dd/MM/yyyy") : string.Empty })<span class="requiredicon"> *</span>
@Html.ValidationMessageFor(model => model.LiveDate)
$(".datepicker").datepicker({
dateFormat: "dd/mm/yy",
showOn: "both"
});
here is the markup for the date field:-
<input Value="24/05/2016" class="datepicker" data-val="true" data-val-date="The field Live Date must be a date." id="LiveDate" name="LiveDate" type="text" value="24/05/2016 00:00:00" /><span class="requiredicon">
now when i try selecting a date from the jQuery date picker such as 17/05/2016
i will get the following error The field Live Date must be a date.
only on Safari & Chrome web browsers, while on IE and Firefox it will work well. also if i select a date such as 01/05/2016
it will work on all the browsers,,, so can anyone advice on this please?
now i read a lot about this problem but could not find any approach that will work in my case.
Solution 1 as mentioned here linke, is to use the following script:-
$(function() {
$.validator.methods.date = function (value, element) {
if ($.browser.webkit) {
//ES - Chrome does not use the locale when new Date objects instantiated:
var d = new Date();
return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
}
else {
return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
}
};
});
but when i added this script inside the scripts section inside my view, i have noted that the client side validation for non-date fields have been disabled also,, which i do not want. for example if i leave a required int field empty , the page will reload before showing the validation error . this will happen when i added the above script, so seems the above script will affect non-date field client side validation also.
Solution 2 . is to use this inside my view, to remove the data-val-date,
$("input[data-val-date]").removeAttr("data-val-date");
now i added this at the end of my view, but it did not disable the client side validation for my Date fields.
so can anyone adivce on this please?