Validate birth date field with javascript

Asked

Viewed 5,007 times

0

I got a field like date, by name txtdata_nasc. I need a validation for it. I need you to have a button next to it that can validate it.

If a person enters a birth date under 18 years of age, I want a alert("Pessoas menores de 18 não podem se cadastrar."), and if he enters a date of birth between the ages of 18 and 60 alert("Maior de 18, pode se cadastrar."). You can’t put a date that makes you over 60.

Please, I want such code to return on a button with the onclick='return validadata()' as an example.

  • Why a button on the side of the field ? Why don’t you just leave the sign up button and validate when submitting or else try using jquery the focus function in the field instead of the button, if it were I would validate with jquery very easy and practical since it uses Ajax.

  • and how I would do that?

  • my sign up button is this: <input type="Submit" value="Register" name="btn1" onclick="Return validatyname();"> only q it already has a function like Return, and I don’t know how to use more than one Return...

  • Related question: https://answall.com/q/297935/132

  • That answer can help you

1 answer

2


You can use this function which checks the age according to the date on input (explanations in the code):

function validadata(){
   var data = document.getElementById("nascimento").value; // pega o valor do input
   data = data.replace(/\//g, "-"); // substitui eventuais barras (ex. IE) "/" por hífen "-"
   var data_array = data.split("-"); // quebra a data em array
   
   // para o IE onde será inserido no formato dd/MM/yyyy
   if(data_array[0].length != 4){
      data = data_array[2]+"-"+data_array[1]+"-"+data_array[0]; // remonto a data no formato yyyy/MM/dd
   }
   
   // comparo as datas e calculo a idade
   var hoje = new Date();
   var nasc  = new Date(data);
   var idade = hoje.getFullYear() - nasc.getFullYear();
   var m = hoje.getMonth() - nasc.getMonth();
   if (m < 0 || (m === 0 && hoje.getDate() < nasc.getDate())) idade--;
   
   if(idade < 18){
      alert("Pessoas menores de 18 não podem se cadastrar.");
      return false;
   }

   if(idade >= 18 && idade <= 60){
      alert("Maior de 18, pode se cadastrar.");
      return true;
   }
   
   // se for maior que 60 não vai acontecer nada!
   return false;
}
<input type="date" name="nascimento" id="nascimento">
<button type="button" onclick='return validadata()'>Validar</button>

  • I need your urgent help on my tcc bro, it’s Pedro Vinicius Aki

  • Bro, I’m making a mistake on my registration page, which I can’t fix, out of nowhere it stopped working

  • you have some email I can send you the page?

  • I sent there, the problem that is giving is in the validation of the date of birth, before it was validating certificate, can not less than 18 years and more than 65, is also in the password, because I created the variable so that can use only letters and numbers in the field, but now it’s returning the Alert to any password, and the zip code, I took a variable on the internet to validate it, but it doesn’t work

  • found some errors, in the end had returning the Function naoNumerico in out of order, ai ajeitei, but msm so the date is not yet working nor the password

  • I see here..

  • worked . . . . . . . . . ... . .. . . .

  • but has no ZIP field.

  • <label for="inputText" class="sr-only">Neighborhood</label> <input type="text" name="txtCep" placeholder="CEP(Ex: ######-####)" class="form-control" maxlength="8" required />

  • Two more things to fix: if (valida_senha || !senha) { and var padrao1 = /[^a-zA-Z0-9]/gi;

  • I put neighborhood by accident, I’m sorry

  • Show, that code saved the day, thanks

Show 8 more comments

Browser other questions tagged

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