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

161
Visualizações
How Can i validate age on date field using vanilla javascript

I am trying to vallidate age on date input field, if somone age in 18+ then he can vallid if he isn’t a 18+ from field will be error how can i do that?

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

0

The following copies a given birthdate into a new constant b18 and varies it by adding 18 years to it. The comparison with the current date then shows whether the person is 18 years old or not.

// const b contains birth date to be tested
document.querySelector("input").addEventListener("input",function(){

 const b=new Date(this.value);

 const b18=new Date(b.getTime());
 b18.setYear(b.getFullYear()+18);

 // alternatively you could calculate that "critical birthdate", 
 // 18 years ago, that makes you an adult today:
 const d18=new Date();
 d18.setYear(d18.getFullYear()-18);

 console.log(new Date()>b18,d18>b);
})
<input type ="date" value="2003-08-29">

about 4 years ago · Juan Pablo Isaza Relatório

0

You can do this:

        <input id="age" type="number" />


        let age = document.getElementById('age');
        let minAge = 18;

        age.onchange = () => {
            if(!age.value >= minAge){
                    age.style.border = "1px solid red";
                } else if(age.value >= minAge) {
                    ​age.style.border = "1px solid green";
                }
        }
 ​
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