Karthick Jayaraman
Karthick Jayaraman

Reputation: 301

Bootstrap DatePicker format not recogined by ASP.NET MVC validator

I'm using bootstrap datepicker in my ASP.NET MVC application. When I try to post the form, I'm getting error saying "The field Start Date must be a date." I'm using "jquery.validate.js"to validate in client side. My date picker format, I have customized as "dd-MM-yyyy" (Eg: 12-May-2016).

This error is happening only in IE, it works fine in Chrome browser.

By googling, I tried different ways:

1) Setting up Data Annotations in Model

[Required]
[Display(Name = "Start Date")]
[DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)]

2) Edited date function in jquery.validate.js

date: function (value, element) {           
            return value.match("^\d{2}-[a-zA-Z]{3}-\d{4}$");
        },

But nothing seems to be working. Most of the questions are discussing around JQuery datepicker, but mine is bootstrap datepicker.

Any help would be appreciated.

Upvotes: 3

Views: 4537

Answers (1)

Archil Labadze
Archil Labadze

Reputation: 4325

There are some fixes:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime BirthDate { get; set; }

2. Go global asax and add:

protected void Application_BeginRequest(Object sender, EventArgs e)
    {

        CultureInfo newCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
        newCulture.DateTimeFormat.ShortDatePattern = "dd/MMM/yyyy";
        newCulture.DateTimeFormat.DateSeparator = "/";
        Thread.CurrentThread.CurrentCulture = newCulture;
    }
  1. And Finaly try this in your View

    @Html.TextBoxFor(model => model.BirthDate, "{0:dd/MM/yyyy}", new {@class = "date-picker form-control"})

Upvotes: 4

Related Questions