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

189
Views
Cannot determine if user input is blank javascript

I am creating a voting system webpage using Javascript. There are several things the user must input before beginning the vote, such as candidates, number of people voting, etc. I have created a function for each one, but when I attempted to add some validating if statements to my enterCandidate function, it would not register an error when the user left the box blank. Here is the function code:

  var enterCandidate = prompt("Please enter a candidate");
  for (let i = 0; i < enterCandidate.length; i++){
    if (isNaN(enterCandidate.charAt(i)) || enterCandidate.charAt(i) == ' '){
      if (!enterCandidate.length == 0){
         candidates.push(enterCandidate);
         console.log(enterCandidate);
         candidate1 = candidates[0];
         candidate2 = candidates[1];
         candidate3 = candidates[2];
         candidate4 = candidates[3];
         candidate5 = candidates[4];
         candidate6 = candidates[5];
      } else {
        alert('Please do not leave the box blank.');
      }
    } else {
        alert('Please enter a name, without symbols or numbers.');
    }
  }
}

The part I am having an issue with is the second if statement, where I used !enterCandidate.length == 0. This should only return true if the user has inputted something but instead it returns false and moves to the else alert 'Please do not leave the box blank'. I am aware that even if this code worked, it would allow people to enter just spaces - I am not really sure how to tackle that without registering errors if people entered full names.

about 4 years ago · Juan Pablo Isaza
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!