Unable to check date format in edit mode in chrome

This question has been asked before, but the answers do not solve my problem. The problem is that I always get the following error in the datetime field.

The Create Time field must be a date.

My attempt

It is modal here where I set the date format

[Display(Name = "Created Time")] [DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] public Nullable<System.DateTime> CreatedTime { get; set; } 

View code

 <div class="form-group"> @Html.LabelFor(model => model.CreatedTime, "Timesheet Date") <div class='input-group date' id='Datetimepicker1'> @Html.TextBoxFor(model => model.CreatedTime, new { @class = "form-control" }) <span class="input-group-addon"> <span class="glyphicon glyphicon-calendar"></span> </span> </div> @Html.ValidationMessageFor(model => model.CreatedTime, "", new { @class = "text-danger" }) </div> 

In js

 $('#CreatedTime').removeAttr("data-val-date"); $('#Datetimepicker1').datetimepicker({ format: 'DD/MM/YYYY' }); 

Also tried changing in jquery.validate.js funcation date: function (value, element) as shown below, but could not solve the problem.

 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)); } 

Visited Links

The field must have a date - date check Picker does not run in Chrome - mvc

Field date must be date in mvc in chrome

And many more links and sites, but unable to solve the problem

+7
javascript jquery date google-chrome asp.net-mvc-5
source share
2 answers

Try

Save this js code outside of $(document).ready(); . only inside the <script>Here</script> .

  $(function () { $.validator.addMethod('date', function (value, element) { if (this.optional(element)) { return true; } var ok = true; try { $.datepicker.parseDate('dd/mm/yy', value); } catch (err) { ok = false; } return ok; }); $("#ID").datepicker({ dateFormat: 'dd/mm/yy', changeYear: true }); }); 

Hope this will be helpful. In my case, it works.

+4
source share

This worked for me:

(Put it on _Layout.cshtml)

 $(function () { $.validator.methods.date = function (value, element) { //Fix chrom Asp.Net MVC jQuery validation: The field "Myfield" must be a date. var ischrom = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase()); if (ischrom) { if ($.browser.webkit) { //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)); } } else { return this.optional(element) || !/Invalid|NaN/.test(new Date(value)); } }; }); 

Credits: http://blog.mohnady.com/2015/02/fix-aspnet-mvc-chrome-jquery-validation.html

+1
source share

All Articles