Validate date of birth, so you can not register under 18 years (Javascript)

Asked

Viewed 1,785 times

-1

I have a field called txtdata_nasc of the date type. I need the user not to be able to type a date that makes him less than 18 years old, but also that he cannot be of an absurd age, such as 100 years old, I need him to be able to type at most a date of birth that makes him 60 years old. For example, it cannot type (using today’s example, 05/05/2018) a date greater than '05/05/2000' and a date shorter than 05/05/1958.

1 answer

1


A simple solution to validate if the person is more or less 18 years old is to create a date with the same month and day, and with the year added up to 18. Then you just need to compare whether that date is shorter or longer than the current date.

Example:

const inputNasc = document.getElementById("txtdata_nasc");

document.querySelector("form").addEventListener("submit", function(){
  //obter array com [ano,mes,dia] através de split("-") e convertendo em numero com Map
  let nasc = inputNasc.value.split("-").map(Number);
  //construir data 18 anos a seguir a data dada pelo usuario
  let depois18Anos = new Date(nasc[0] + 18, nasc[1] - 1, nasc[2]);
  let agora = new Date();
  
  if (depois18Anos <= agora){
    console.log("Maior de 18");
  }
  else {
    console.log("Menor de 18");
  }
  event.preventDefault(); //só para não mudar de pagina na submissão do formulario
});
<form>
  <input type="date" id="txtdata_nasc">
  <input type="submit" value="Validar">
</form>

It is important to note that as Javascript months start at zero, the part of the month has to be subtracted from 1 with nasc[1] - 1.

To validate for less than 100 years can use exactly the same principle, changing only in comparison:

let nasc = inputNasc.value.split("-").map(Number);
let depois18Anos = new Date(nasc[0] + 18, nasc[1] - 1, nasc[2]);
let depois100Anos = new Date(nasc[0] + 100, nasc[1] - 1, nasc[2]);
let agora = new Date();

if (depois18Anos > agora){
  console.log("Menor de 18");
}
else if(depois100Anos < agora){
  console.log("Maior de 100");
}
else {
  console.log("valido");
}

With destructuring assignment can make the construction of dates X years ahead simpler and clearer:

let [ano, mes, dia] = inputNasc.value.split("-").map(Number);
//    ^----^----^--- Destructuring assignment
let depois18Anos = new Date(ano + 18, mes - 1, dia);
let depois100Anos = new Date(ano + 100, mes - 1, dia);
  • Is there any way you can help me with my Course Completion Work (TCC)? I’m working with Javascript, but I’m having a lot of difficulties.

  • @Pedrovinicius Not really because it is not the purpose of the site. Here the goal is to clarify specific doubts that have in the scope of programming, and in this sense I will be happy to help.

  • I know it’s not the purpose of the site, it was just a question.

  • I would like to know how I do now to use this code on my Site. I must create a Function?

  • I have a registration page, where there is a "register" button at the end, which executes all validations, and has the onclick="Return validatyname();" method which is the Function I use to validate the rest of the site.

  • Is there any way I can put this code of yours back on my sign-up button?

  • Sorry for the question, I really have difficulty in JS as I had to learn everything over the Internet to perform my TCC. And as you know, on the Internet there’s not much about Javascript that helps me.

  • All of these questions you ask should be new questions, assuming that there are no longer equal questions on the site that answer exactly what you want. Soon the first thing to do is to look for identical questions and if you do not find a new.

Show 3 more comments

Browser other questions tagged

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