I am using Asp.Net Mvc3 and the unobtrusive jquery validation. I\'d like to have my dates validation localized, I mean, jquery is validating my date as being MM/dd/yyyy but
Little correction of Johnny Reilly answer:
$.validator.methods.number = function (value, element) {
if (Globalize.parseFloat(value)) {
return true;
}
return false;
}
must be replaced with
$.validator.methods.number = function (value, element) {
return !isNaN(Globalize.parseFloat(value));
}
for correct parsing of zero string ("0").
So entire code is:
///
///
///
/*!
* Monkey patch for jquery.validate.js to make use of Globalize.js number and date parsing
*/
$(document).ready(function () {
var currentCulture = $("meta[name='accept-language']").prop("content");
// Set Globalize to the current culture driven by the meta tag (if any)
if (currentCulture) {
Globalize.culture(currentCulture);
}
//Tell the validator that we want numbers parsed using Globalize.js
$.validator.methods.number = function (value, element) {
return !isNaN(Globalize.parseFloat(value));
}
//Tell the validator that we want dates parsed using Globalize.js
$.validator.methods.date = function (value, element) {
if (Globalize.parseDate(value)) {
return true;
}
return false;
}
//Fix the range to use globalized methods
jQuery.extend(jQuery.validator.methods, {
range: function (value, element, param) {
//Use the Globalization plugin to parse the value
var val = Globalize.parseFloat(value);
return this.optional(element) || (val >= param[0] && val <= param[1]);
}
});
});