Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Validation and MVC 3. How to change date format

I'm a newbie to MVC 3 and JQuery Validation so any help I can get here will be very much appreciated.

My devleopment platform is .NET MVC 3 website. I'm using the built in unobtrusive javascript for form validation. Is there a way to change the date to a different format for a valid date. As far as I can tell, the valid format is dd/mm/yy. Is it possible to change the valid date format to something like "Apr 3, 2012"?

My view model has a field

[Required]
DateTime OrderDate { get; set; }

I know that MVC 3 is using jquery validation under the hood so I'm thinking the solution will require a change to jquery validate and also not sure how to hook it up to MVC so it works like all the other built in data validations using data annotations.

Thank you.

like image 592
user1279723 Avatar asked Mar 19 '12 23:03

user1279723


1 Answers

When you use client side validation for date, you have to override the jQuery validation for date as well.

$.validator.methods.date = function (value, element) {
    return this.optional(element) || Globalize.parseDate(value, "MMM dd, yyyy") !== null;
}

You have to reference the Globalize library and the appropriate culture in your HTML head. Download from https://github.com/jquery/globalize.

like image 128
Jeow Li Huan Avatar answered Oct 01 '22 05:10

Jeow Li Huan