iBoonZ
iBoonZ

Reputation: 1035

ASP MVC 3 Client side date localization validation

I am currently busy on a Asp.net MVC 3 website. There I have a dropdown that sets the culture (nl-BE // en-US)

When I try to parse a date with format 'dd/mm/yyyy' with nl-BE culture, on the SERVER, everything works, but on the client my browser (chrome) keeps on saying that the dateformat "dd/mm/yyyy" is not a correct dateformat. (client side validation)

I tried already with the 'globalize.js' from jquery but stil no succes.

The only way I can fool my browser is to manually add my own validator jQuery.validator.addMethod( 'date', function (value, element, params) { return Date.parseExact(value, "d/M/yyyy"); });

But when I enter following date:25/05/2012 for nl-BE, the browser says ok, but my server throws an error, ( because this wrong format) so its not error proof.

Could someone help for a error proof client side validation that can process dd/mm/yyyy or mm/dd/yyyy

Thanks

Upvotes: 0

Views: 3842

Answers (2)

iBoonZ
iBoonZ

Reputation: 1035

This is a bug in Chrome, I found the soltion here ))

http://geekswithblogs.net/EltonStoneman/archive/2009/10/29/jquery-date-validation-in-chrome.aspx

Maybe some people will get the error too ;)

Upvotes: 1

Pieter Vugts
Pieter Vugts

Reputation: 23

You could force it in your Object property.

[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]

if you combine this with the jQuery UI datepicker, so that the site visitor doesn't type a date, but picks one, it should be solid.

the final thing you would need is within your shared folder, a new folder : EditorTemplates with a new view: DateTime.cshtml:

@model System.DateTime 
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, 
                    new { data_datepicker=true })

Upvotes: 1

Related Questions