Do not allow retroactive date in Date field

1

The field of type date can not allow retroactive dates. I need the script to recognize the current date and not allow earlier dates.

<div class="col-sm-4">
    <label for="ultimoDiaTrab" class="control-label">Último dia a ser trabalhado:</label>
    <input type="date" id="ultimoDiaTrab" name="ultimoDiaTrab" class="form-control" disabled />                         
</div>
    
asked by anonymous 14.03.2017 / 15:22

1 answer

2

You can do it like this:

var input = document.getElementById('ultimoDiaTrab');
input.addEventListener('change', function() {
  var agora = new Date();
  var escolhida = new Date(this.value);
  if (escolhida < agora) this.value = [agora.getFullYear(), agora.getMonth() + 1, agora.getDate()].map(v => v < 10 ? '0' + v : v).join('-');
});
<div class="col-sm-4">
  <label for="ultimoDiaTrab" class="control-label">Último dia a ser trabalhado:</label>
  <input type="date" id="ultimoDiaTrab" name="ultimoDiaTrab" class="form-control" />
</div>

In this case this.value = [agora.getFullYear(), agora.getMonth() + 1, agora.getDate()].map(v => v < 10 ? '0' + v : v).join('-'); resets the chosen date with the current date, if you just want to delete you can do this.value = '';

    
14.03.2017 / 15:29