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?
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.