assign validation attributes to html date with javascript

0

Good afternoon. Does anyone know how to correctly apply this function? I found a code in this same place that allowed me to enter a value of "today" at least for a date in HTML

<input id="datefield" type='date' min='1899-01-01' max='2000-13-13'></input>

The detail is that you create another date, but this time I want this date to have as a minimum parameter the date assigned to the first date

<input id="datefield1" type='date' min='1899-01-01' max='2000-13-13'></input>

and this is my javascript

<script type="text/javascript">
window.onload=function(){ 
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //enero es 0
var yyyy = today.getFullYear();
 if(dd<10){
        dd='0'+dd
    } 
    if(mm<10){
        mm='0'+mm
    } 


today = yyyy+'-'+mm+'-'+dd;
/* hasta aquí todo sirve para asignar a today la fecha de hoy*/
document.getElementById("datefield").setAttribute("min", today);
/* se asigna la fecha minima para el atributo datafield*/
var x = document.getElementById("datefield").value; // aqui trato de obtener la fecha de datafield
document.getElementById("datefield1").setAttribute("min", x); //y aqui trato de asignar la fecha almacenada en x como fecha minima
}
</script>

I also attach the fiddle to see my progress working. link

Best regards!

    
asked by ulises.isc 08.12.2018 в 21:51
source

1 answer

1

You can use an event listener on the datefield, so every time the datefield loses the focus, the min attribute of the datefield will be added to datefield1.

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


   today = yyyy+'-'+mm+'-'+dd;
   /* hasta aquí todo sirve para asignar a today la fecha de hoy*/
   document.getElementById("datefield").setAttribute("min", today);
   /* se asigna la fecha minima para el atributo datafield*/




  document.getElementById("datefield").addEventListener('focusout',function(){
        var x = document.getElementById("datefield").value;

        document.getElementById("datefield1").setAttribute("min", x); 
    });

}
    
answered by 08.12.2018 / 22:48
source