VALIDATOR BY AGE

Asked

Viewed 46 times

0

Hello I don’t have much knowledge of javascript, I appreciate if anyone can help me.

I need to create a simple validation page by age to access a page, the idea is simple: if the person was born before 2002 redirects the page meusite_com_br/major but if she was born after 2002 redirects the page meusite_com_br/minor

I found the code below that I made some adjustments, but I do not know how to open the respective pages meusite_com_br/minor or meusite_com_br/major as a result, the result in this case are texts: "LOWER 18" : "HIGHER OF 18"

<html>
<body>
<CENTER>
<h2>Validador de Idade</h2>

<p>Digite o ano que você nasceu para acessar o site:</p>

<input id="age" value="Ex: 1990" />
<br>
<button onclick="myFunction()">ACESSAR SITE</button>

<p id="demo"></p>

<script>
function myFunction() {
  var age, voteable;
  age = Number(document.getElementById("age").value);
  if (isNaN(age)) {
    voteable = "Você não inseriu um ano válido. <BR> Exemplo: 2020";
  } else {
    voteable = (age > 2002) ? "MENOR 18" : "MAIOR DE 18";
  }
  document.getElementById("demo").innerHTML = voteable;
}
</script>
</CENTER>
</body>
</html>

Thanks again for the help! Luis

1 answer

3


Just use window.location with the desired address, thus:

  var age = Number(document.getElementById("age").value);

  if (isNaN(age)) {
    document.getElementById("demo").innerHTML = "Você não inseriu um ano válido. <BR> Exemplo: 2020";
  } else if (age > 2002) {
    window.location = "/menor";
  } else {
    window.location = "/maior";
  }

ps: it was not necessary to declare the variables before, could declare and already set, and in the case of the variable voteable nor need for the case of the value is numeric, because already it will redirect, will not even give much time to read the message

  • could have helped in if (age > 2002) ... for the year has to change :P -> if(age > (new Date().getFullYear() - 18))...

  • @balexandre this goes beyond what is asking, the problem of the question is redirecting, not how the logical part works, but agree that it would be an improvement, although front-end validation is rarely a good one, only being interesting to avoid unnecessary HTTP requests and even customize a little the visual.

  • William and Alexander. Grateful, I tested the amendment that William sent and worked perfectly, of course I asked for the basics to have the function that I need and it was already perfect, but of course improvements are always welcome, for not understand much of the subject as I said earlier, I couldn’t perform the improvement that Alexandre went through and of course visually I know it’s not pretty, but how much I need a validator that leads to different paths depending on the answer and that code was the only one I could. This code I picked up in w3schools.com/js/js_comparisons.Asp

  • @Luis validation is relative, your case is not even validate, the question is more for the use of Ifs and how to redirect, validate would be something more complex and surely would have to be done on the backend and if possible with more reliable data than the user himself say the year he was born, because in this way anyone can bypass the system, but anyway the question is about ifs and redirection, and I think this I already answered, if you can mark as correct chance have solved I will be grateful, and if you have a new doubt of truth validation open a new question.

  • @Guilhermenascimento is no doubt answered and classified and I am grateful. I certainly understand that it is not a validation but a content warning speaking in a layman’s way.

Browser other questions tagged

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