Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda