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

139
Visualizações
Validate javascript and html form

I'm doing an exercise but I can't validate and send a form.

I have this HTML code, to which I cannot add or modify anything:

</head>
  <body>
    <h1>Card game</h1>

    <p>
      <label>Displays the name of the participant</label
      ><input type="text" name="name" />
    </p>
    <p>
      <label>how many games do you want to play? </ tag
      ><input type="number" name="games" value="0" />
    </p>
    <button>PARTICIPATE!</button>


   <script type="text/javascript" src="rockPaperScissors.js"></script>
  </body>

I am trying to validate the form, which should turn the fields red when I hit the participate button and does not meet the validation conditions. Once the data has been corrected and the form has been validated, the fields must be deactivated so that they cannot be written again and remain visual.

I have done this but I don't get my goal:

function validateName() {
  const name = document.getElementsByName("name");
  const expression1 = /[A-Za-z]{3,}/;
  name.click();

  if (!expression1.test(name.value)) {
    name.classList.add("RedBackground");
    false return;
  }
  return true;
}

function validateGames() {
  games.click();
  const items = document.getElementsByName("items");
  if (games.value <= 0) {
    games.classList.add("RedBackground");
    false return;
  }
  return true;
}

// Indicate who launches the events
document
  .getElementsByTagName("button")[0]
  .addEventListener("click", validateName);

document
  .getElementsByTagName("button")[0]
  .addEventListener("click", validateGames);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Issue

<input type="text" name="name" />
const name = document.getElementsByName("name");

getElementsByName returns a collection and not a single element.

Possible solution

To get the first element whose name attribute equals name, you have to either get the first index of the collection:

const name = document.getElementsByName("name")[0];

or querySelector which returns the first matching element.

const name = document.querySelector("[name=name]");

Example

document.querySelector("button").addEventListener("click", function(){
  //REM: Is everything valid?
  let tValid = true;

  //REM: Validate "name" using "querySelector"
  let tName = document.querySelector("[name=name]");
  if(tName){
    //REM: Put your logic here
    tName.classList.add("RedBackground");
    tValid = false
  };
  
  //REM: Validate "games" using "getElementsByName"
  let tGames = document.getElementsByName("games")[0];
  if(tGames){
    //REM: Put your logic here
    tGames.classList.add("BlueBackground");
    tValid = false
  };
  
  return tValid
});
.RedBackground{
  background-color: crimson
}

.BlueBackground{
  background-color: cornflowerblue
}
<h1>Card game</h1>
  <p>
    <label>Displays the name of the participant</label>
    <input type="text" name="name" />
  </p>
  <p>
    <label>how many games do you want to play? </ tag>
    <input type="number" name="games" value="0" />
  </p>
  <button>PARTICIPATE!</button>

I recommend, even if possible, not to reuse the keyword name as variable name.

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