Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery validate future date addMethod();

Some question for jquery validate plugin and only date in future.

I've got an input field like this:

<input id="date_hinfahrt" class="gui-input error" type="date" required="" name="datefuture" placeholder=" DD/MM/YYYY" aria-required="true" aria-invalid="true">

please, I dont want the datepicker in it!! There is only a mask like this:

 $("#datefuture").mask('99/99/9999', {placeholder:'_'});

And for the validate plugin I've try this addMethod(); but it's not working and I hope someone can help me.

$.validator.addMethod("minDate", function(value, element) {
    var now = new Date(); 
    var myDate = new Date(value);
    var mD = ("0" + (myDate.getDate())).slice(-2) + '/' + ("0" + (myDate.getMonth()+1)).slice(-2) + '/' + myDate.getFullYear();
    var nowdate = ("0" + (now.getDate())).slice(-2) + '/' + ("0" + (now.getMonth()+1)).slice(-2) + '/' + now.getFullYear();
    return this.optional(element) || mD > nowdate;
    // else alert('Das passt nicht!' + mD +  '   ' + nowdate);
});
like image 601
DieAndere Avatar asked Mar 18 '23 05:03

DieAndere


1 Answers

You can just compare dates directly, you don't need to do all that concatenation.

jQuery.validator.addMethod("minDate", function (value, element) {
    var now = new Date();
    var myDate = new Date(value);
    return this.optional(element) || myDate > now;

    // else alert('Das passt nicht!' + mD +  '   ' + nowdate);

});

There were also problems in the way you set up your fiddle. You had name="date_hinfahrt", but in your rules: option you used datefuture. Also, formtools.js doesn't work with type="date" inputs, so it was emptying the field, causing it always to complain that the field is required; I changed it to type="text".

FIDDLE

like image 50
Barmar Avatar answered Mar 23 '23 07:03

Barmar