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.
– Pedro Vinicius
@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.
– Isac
I know it’s not the purpose of the site, it was just a question.
– Pedro Vinicius
I would like to know how I do now to use this code on my Site. I must create a Function?
– Pedro Vinicius
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.
– Pedro Vinicius
Is there any way I can put this code of yours back on my sign-up button?
– Pedro Vinicius
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.
– Pedro Vinicius
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.
– Isac