Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery dd/MM/yyyy date format validation

I using default ASP.NET MVC 4 validation bundle in my application. In view I have a date field in the format of "dd/MM/yyyy" and jquery validation failed to validate the format. Then I added the below code to override the default behavior.

$(function () {
        $.validator.methods.date = function (value, element) {
            Globalize.culture("en-GB");
            // you can alternatively pass the culture to parseDate instead of
            // setting the culture above, like so:
           // parseDate(value, null, "en-AU")
            return this.optional(element) || Globalize.parseDate(value) !== null;
        }
    });

Then the date validation issue was resolved, and now my application does not fire client side validation but the server side validation. what could be the reason for this behavior?

like image 448
chamara Avatar asked Dec 31 '13 06:12

chamara


2 Answers

Found a simpler solution for me on see : Clientside validation fails for date format dd/mm/yyyy in jQuery Validate

Like it because I don't need new third party scripts or any extra plugins

Basically overwrite the validation method like this:

$(document).ready(function () {
    $.validator.methods.date = function(value, element) {
        return this.optional(element) || parseDate(value, "dd-MM-yyyy") !== null;
    };
});
like image 192
Dai Bok Avatar answered Oct 14 '22 14:10

Dai Bok


You can use dateITA from additional methods.

http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/additional-methods.js

$("#myform").validate({
    rules: {
        dateITA: true
    }
})

I hope it works.

like image 32
jcamelis Avatar answered Oct 14 '22 14:10

jcamelis