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?
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;
};
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With