0
All right with you guys?!
You could help me with a situation.
I’m making a simple form, in it I have two inputs, one of them is the Date of Birth and the other is Age. What I am wanting to do is that when I type the date of birth in the first input the second automatically pulls the Age already calculated.
Ex: when I type in Input date of birth : 09/01/2001
the input of the Age automatically bring me 18 years old without pressing a button
In my code, I can actually pull the calculated age by following the above example, man input gosh 18, but I’m not getting to make that pull "18 years old". In case I wish to add "years old" right after the age you’re pulling.
Follow the code:
HTML
<form method="get" action="evento.php?cadastro=true">
    <div>
        <fieldset>
            <legend>DADOS GERAIS</legend>
             <p>Nome * <input type="text" name="nomecompleto" required="true">
                Sexo * <select name="sexo">
                 <option value="">Selecione...</option>
                 <option value="Masculino">Masculino</option>
                 <option value="Feminino">Feminino</option>
             </select>
             Cpf * <input type="text" name="cpf" required="true">
            </p>
            <p>Data <input type="text" name="nascimento" id="data" style="width: 110px;" >
              Idade <input type="text" name="idade" id="idade" disabled style="width: 70px;">
            </p>
        </fieldset>
</div>
    </form>JS
 document.getElementById("data").addEventListener('change', function() {
  var data = new Date(this.value);
  if(isDate_(this.value) && data.getFullYear() > 1900)
      document.getElementById("idade").value = calculateAge(this.value);
});
function calculateAge(dobString) {
  var dob = new Date(dobString);
  var currentDate = new Date();
  var currentYear = currentDate.getFullYear();
  var birthdayThisYear = new Date(currentYear, dob.getMonth(), dob.getDate());
  var age = currentYear - dob.getFullYear();
  if(birthdayThisYear > currentDate) {
    age--;
  }
  return age;
}
function calcular(data) {
  var data = document.form.nascimento.value;
  alert(data);
  var partes = data.split("/");
  var junta = partes[2]+"-"+partes[1]+"-"+partes[0];
  document.form.idade.value = (calculateAge(junta));
}
var isDate_ = function(input) {
        var status = false;
        if (!input || input.length <= 0) {
          status = false;
        } else {
          var result = new Date(input);
          if (result == 'Invalid Date') {
            status = false;
          } else {
            status = true;
          }
        }
        return status;
}
Thanks a lot for the help. I’m a bit new in the programming area, I didn’t know about the
readonlyI will focus on reading more about the possibilities I have. Thank you for making me evolve one more step in this area.– Leonardo012