vendredi 3 décembre 2021

Need the output of the inputs to be pushed into the

I'm trying to get the user inputs to be pushed out as a message into a <div> via the button press after confirming that all the inputs are filled and the correct password has been entered. That part works fine but my forEach is not working:

If the user has input a number value lower than 18, I want an if that types out via push "Your name is UName and you are Age years old, you are not of legal age", or else the same result as above but ending with "...you are of legal age".

Any idea how I should go forth?

let nameElement = document.getElementById("UNameInput");
let faultElement = document.getElementById("errorOutput");
let ageElement = document.getElementById("AgeInput");
let clickElement = document.getElementById("button");
let passwordElement = document.getElementById("PasswordInput");
clickElement.addEventListener("click", function(e) {

  let feedback = [];
  if (UNameInput.value === '' || UNameInput.value === null) {
    feedback.push('Name input missing')
  }

  if (AgeInput.value === '' || AgeInput.value === null) {
    feedback.push('Age input missing')
  } else if (PasswordInput.value !== 'IHM') {
    feedback.push('Password is not valid')
  } else {
    feedback.forEach((item, i) => {
      if (item.AgeInput < 18) {
        feedback.push("You are not of not legal age")
      } else {
        feedback.push(" You are of legal age ")
      }
    });
  }
  if (feedback.length > 0) {
    e.preventDefault()
    faultElement.innerText = feedback.join(", ")
  }
});
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="JSny.js" defer></script>
</head>

<body>
  <div id="errorOutput"></div>
  <p id="output"></p>
  <div>
    <label for="UNameInput">Name Input</label>
    <input id="UNameInput" name="name" type="text">
  </div>
  <div>
    <label for="AgeInput">Age Input</label>
    <input id="AgeInput" name="age" type="number">
  </div>
  <div>
    <label for="PasswordInput">Password Input</label>
    <input id="PasswordInput" name="password" type="password">
  </div>
  <button id="button">Submit</button>
</body>
</html>

Aucun commentaire:

Enregistrer un commentaire