Override jquery date

I found this useful bit of code that allows you to use uk dates in Chrome, but I'm not sure how to implement it. It overrides the default functionality.

date: function(value, element) { //ES - Chrome does not use the locale when new Date objects instantiated: //return this.optional(element) || !/Invalid|NaN/.test(new Date(value)); var d = new Date(); return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value))); }, 

How can I add this to jQuery validation to override the default functionality.

This is where I found the sample code

+6
source share
1 answer

You must call the validator.addMethod method after loading the jquery.validate library as follows:

 $(function () { // Replace the builtin US date validation with UK date validation $.validator.addMethod( "date", function (value, element) { var bits = value.match(/([0-9]+)/gi), str; if (!bits) return this.optional(element) || false; str = bits[1] + '/' + bits[0] + '/' + bits[2]; return this.optional(element) || !/Invalid|NaN/.test(new Date(str)); }, "Please enter a date in the format dd/mm/yyyy" ); }); 

Please note that I used a different way of actually checking input, as the code in your question will not work (toLocaleDateString does not accept the parameter). You can also change this to use the datejs library, as Mr. Lord noted in the comments.

+16
source

Source: https://habr.com/ru/post/893825/


All Articles