Jquery validation for future date

Hi I need to validate a date that has to be only valid if is in the future, in the form I’m using the date format gg/mm/yyyy using Jquery validation plugin with dateITA rule. I’ve added the following validation method

jQuery.validator.addMethod(“future”, function(value, element) {
return this.optional(element) || Date.parse(value) < new Date().getTime(); //use Date.now() if you can instead of getTime, it’s nicer
}, “Please enter only future dates”);

It only works if dates are in the american format like gg-mm-yyyy
Ho can i make it work for gg/mm/yyyy?

Many thanks for your help

you have to re-format your date string into a format that the date parser understands. preferably an ISO date (yyyy-mm-dd).

Hi thanks for your help, I’ve searched on google and I can’t find any info how to re-format the date. Could you help me?

hm. you hame dd/mm/yyyy and you want yyyy-mm-dd, what whould you logically do if you want to transform one into the other?

Hi, thanks for your help, I’ve tried to use moment.js to convert today date in the format dd/mm/yyyy but still doesn’t work.


jQuery.validator.addMethod("futuredocexp", function (value, element) {
    var now = moment(new Date()).format("DD/MM/YYYY");
    var myDate = new Date(value);
    return this.optional(element) || myDate > now;
});

you’re doing it backwards. it’s not the current time that needs formatting, it’s the input value that needs formatting. As I already said DD/MM/YYY is not a format Date() understands because it’s ambiguous.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.