Return to Snippet

Revision: 64087
at July 3, 2013 18:07 by SnipplrSlush


Initial Code
// you need globalize.js and globalize.culture.de-DE.js for this example.
// this overrides the default methods of jQuery Validate so load it only if you want to
// use german globalization.  

$.validator.methods.number = function (value, element) {
    return this.optional(element) || !isNaN(Globalize.parseFloat(value));
};

$.validator.methods.date = function (value, element) {
    var check = false;
    var re = /^\d{1,2}\.\d{1,2}\.\d{4}$/;
    if (re.test(value)) {
        var adata = value.split('.');
        var dd = parseInt(adata[0], 10);
        var mm = parseInt(adata[1], 10);
        var yyyy = parseInt(adata[2], 10);
        var xdata = new Date(yyyy, mm - 1, dd);
        if ((xdata.getFullYear() == yyyy) && (xdata.getMonth() == mm - 1) && (xdata.getDate() == dd))
            check = true;
        else
            check = false;
    } else
        check = false;
    return this.optional(element) || check;
};

$(function () {
    Globalize.culture('de-DE');
});

Initial URL

                                

Initial Description
jQuery Validate is used in ASP.Net MVC 4. As it is checking only en-US as a default you may have problems with different cultures for decimal inputs and dates (I had to try for minutes to find a accepted date format. I don't want to see how my customers would react). The decimal has been converted from the accepted 9.90 to 990,00 by the controller.

Initial Title
Globalize jQuery Validate for ASP.Net MVC 4

Initial Tags
javascript, jquery, aspnet

Initial Language
JavaScript