Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

292
Views
How do i display a single alert for multiple fields being empty?

I have a form with first name, last name and email, and i need to make an alert if a field is empty. I made an alert but it comes up for every field that is empty.

This is the js:`

document.querySelector("#book").addEventListener("click", () => {
  let inp = document.querySelectorAll(".form-control");
  for (let i = 0; i < inp.length; i++) {
    if (inp[i].value == "") {
      alert("All fields must be filled!");
    }
  }
});

`

The form-control class is on all the input fields. If i leave all 3 inputs empty, the alert comes up 3 times.

Please help, my brain is stuck.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

you can use array.some on inputs get by querySelectorAll() to raise only one alert if one fields is not empty

document.querySelector("#book").addEventListener("click", () => {
  let inputs = document.querySelectorAll(".form-control");
  if ([...inputs].some(input => input.value === '')) {
      alert("All fields must be filled !");
  }
});
<input class="form-control" />
<input class="form-control" />
<button id="book">validate</button>

about 4 years ago · Juan Pablo Isaza Report

0

You can simply use an array to get all errors inside a single array and after the loop finish then you can give the final alert.

document.querySelector("#book").addEventListener("click", () => {
  let inp = document.querySelectorAll(".form-control");
  let errors = [];
 
  for (let i = 0; i < inp.length; i++) {
    if (inp[i].value == "") {
       errors.push("Error "+ i);
    }
  }

  if(errors != []){
       alert("All fields must be filled!");
  }
});
about 4 years ago · Juan Pablo Isaza Report

0

If you want a generic message, you can just display it once, and even stop the loop:

document.querySelector("#book").addEventListener("click", () => {
  let inp = document.querySelectorAll(".form-control");
  for (let i = 0; i < inp.length; i++) {
    if (inp[i].value == "") {
      alert("All fields must be filled!");
      break; // <-- alert was shown, nothing else to do
    }
  }
});

If you want to show a single message, but specific to the missing field(s), you have to collect them in the loop, and show the single message after, something like

document.querySelector("#book").addEventListener("click", () => {
  let inp = document.querySelectorAll(".form-control");
  let missing=[];
  for (let i = 0; i < inp.length; i++) {
    if (inp[i].value == "") {
      missing.push(inp[i].name);
    }
  }
  if(missing.length) {
    alert("Please fill the following fields too: "+missing.join());
  }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!