Disable dates greater than the current one in an input date

0

I have a form which has a input type="date" I would like the dates after the current date to be disabled How could I do it?

    
asked by jaoc6 19.07.2018 в 17:48
source

2 answers

1

You can use the attribute max="xxxx-xx-xx" in the input tag. If it's php you can use

max="<?php echo date(Y-m-d)?>"

With javascript you can use (Source: link ) :

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();
 if(dd<10){
        dd='0'+dd
    } 
    if(mm<10){
        mm='0'+mm
    } 

today = yyyy+'-'+mm+'-'+dd;
document.getElementById("datefield").setAttribute("max", today);

I hope you serve

    
answered by 19.07.2018 / 17:56
source
1

You can do it by adding the attribute max or min , I'll give you the example:

<input name="somedate" type="date" min="2018-07-19">

But if you want it to be based on the current day, you can do it in the following way:

    var today = new Date().toISOString().split('T')[0];
    document.getElementsByName("somedate")[0].setAttribute('max', today);
<input name="somedate" type="date">

It would be a matter of changing the max by min or vice versa depending on whether you want today to be the minimum or maximum date.

I hope it is of your help. Greetings.

    
answered by 19.07.2018 в 18:01