How to block retroactive dates in input datetime?

Asked

Viewed 487 times

0

I have a form with a input of the kind datetime-local and I need to block the insertion of retroactive dates, how do I do this?

<form name="F1" id="edit-profile" class="form-horizontal" action="" method="post" enctype="multipart/form-data">
  <div class="control-group">
    <label class="form-space control-label" for="lastname">Titulo: </label>
    <input type="text" class="form-space form-control form-margin disable" name="titulo" id="titulo" required="" value="">
    <label class="form-space control-label" for="lastname">Subtitulo: </label>
    <input type="text" class="form-space form-control form-margin" id="subtit" value="" name="subtit" required="">
    <label class="form-space control-label" for="lastname">Caso deseja agendar, insira uma data e hora: </label>
    <input type="datetime-local" class="form-space form-control form-margin" id="agend" value="" name="agend">
    <input type="submit" name="cadastro" value="Enviar" class="btn-post btn">
  </div>
</form>
  • Do you want to block dates from a month, a specific day, or dates prior to today? Explain better what you want to do.

1 answer

0

Try it this way. Note: Does not validate the hours, only the day.

$("#edit-profile").submit(function(e) {

  var agendamento = $("#agend").val().split('T');
  agendamento = new Date(agendamento[0]).setHours(24);
  var hoje = new Date();
  if (agendamento <= hoje) {
    alert('Por favor, insira uma data válida!');
    e.preventDefault();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="F1" id="edit-profile" class="form-horizontal" action="" method="post" enctype="multipart/form-data">
  <div class="control-group">
    <label class="form-space control-label" for="lastname">Titulo: </label>
    <input type="text" class="form-space form-control form-margin disable" name="titulo" id="titulo" required="" value="">
    <label class="form-space control-label" for="lastname">Subtitulo: </label>
    <input type="text" class="form-space form-control form-margin" id="subtit" value="" name="subtit" required="">
    <label class="form-space control-label" for="lastname">Caso deseja agendar, insira uma data e hora: </label>
    <input type="datetime-local" class="form-space form-control form-margin" id="agend" value="" name="agend">
    <input type="submit" name="cadastro" value="Enviar" class="btn-post btn">
  </div>
</form>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.