Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138192

forEach loop does not check all the items in the array if a condition is met in any of them

$
0
0

I have a forEach loop for an array but with an if statement to check if the input is empty. The problem is that the loop stops if it finds an empty input i want it to check all the element in the array even if 1 is empty

here is my code

function showError(input, message) {
  const formControl = input.parentElement;
  formControl.className = "form-control error";
  const small = formControl.document.querySelector("small");
  small.inerText = message;
}
//show success message
function showSuccess(input) {
  const formControl = input.parentElement;
  formControl.className = "form-control success";
}

//check requiered fields

function checkRequired(inputArr) {
  inputArr.forEach(function(input) {

    if (input.value.trim() === "") {
      showError(input, "is required");
    } else {
      showSuccess(input);
    }
  });
}

// Event listener
form.addEventListener("submit", function(e) {
  e.preventDefault();
  checkRequired([username, email, password, password2]);
});

enter image description here]


Viewing all articles
Browse latest Browse all 138192

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>