Sunday, February 8, 2015

Fix Asp.Net MVC chrome jQuery validation: The field "MyField" must be a date.

Fix Asp.Net MVC chrome jQuery validation: The field "MyField" must be a date.

- just override jquery date validation function :

$(function () {
            $.validator.methods.date = function (value, element) {
                //Fix chrom Asp.Net MVC jQuery validation: The field "Myfield" must be a date.
                var ischrom = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase());
                if (ischrom) {
                    if ($.browser.webkit) {
                        //Chrome does not use the locale when new Date objects instantiated:
                        var d = new Date();
                        return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value)));
                    }
                    else {
                        return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
                    }
                }
                else
                {
                    return this.optional(element) || !/Invalid|NaN/.test(new Date(value));
                }
            };

        });

2 comments:

Unknown said...

Thanks you, that worked with me !

Hugh said...

$.browser was deprecated in jQuery 1.9