Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

91
Visualizações
JS/HTML Date picker enforce rule to ensure input is today

When creating a date picker, I have it set to only allow today onwards. which works... But it appears that the user can then tab into the field and change the date to "before" today. Is there a way to enforce the "must be today or future date" rule?

Here is my example;

var today = new Date();
var d = today.getDate();
var m = today.getMonth() + 1;
var y = today.getFullYear();
if (d < 10) {
  d = '0' + d
}
if (m < 10) {
  m = '0' + m
}

today = y + '-' + m + '-' + d;
document.getElementById("datePicker").setAttribute("min", today);
<input id="datePicker" type='date' >

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

use EventListener to listen and set the value of your input to today date when entered value is lower than today

    var today = new Date();
    var d = today.getDate();
    var m = today.getMonth() + 1;
    var y = today.getFullYear();
    if (d < 10) {
      d = '0' + d
    }
    if (m < 10) {
      m = '0' + m
    }

    today = y + '-' + m + '-' + d;
    const input = document.getElementById("datePicker")
    input.setAttribute("min", today);

    input.addEventListener('change', (event) => {
      const d1 = new Date(today);
      const d2 = new Date(event.target.value);
      if(d1.getTime() > d2.getTime()){
        event.target.value = today
      }
    });
<input id="datePicker" type='date'>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda