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

288
Visualizações
html javascript form no longer working since adding new input field

I have the following html and javascript code for validation on the input fields, this was working with the one input field for first name but since I tried to extend my code by adding a new input field for last name now the form validation has stopped working as follows:

function myFunction() {

  let x = document.getElementsByName("first_name").[0]value;
  let y = document.getElementsByName("last_name")[0].value;

  let text;
  text = "";
  
  if (x == '' ||  x == null) {
    text = "Input not valid";
  }
  document.getElementById("first_name_errors").innerHTML = text;
}

  if (y == '' ||  y == null) {
    text = "Input not valid";
  }
  document.getElementById("last_name_errors").innerHTML = text;
}


document.addEventListener('invalid', (function () {
  return function (e) {
    e.preventDefault();
    document.getElementsByName("first_name").focus();
    document.getElementsByName("last_name").focus();
  };
})(), true);

</head>
<body>

<input type="text" name="first_name"  placeholder="first name" name class="input_fields" required>
<div class="error-message" id="first_name_errors"></div>
      <input class="save_btn" type="submit" value="Save" name="save_fname" onclick="myFunction()">

<br><br>

<input type="text" name="last_name"  placeholder="last name" name class="input_fields" required>
<div class="error-message" id="last_name_errors"></div>
      <input class="save_btn" type="submit" value="Save" name="save_lname" onclick="myFunction()">

How can I get this back working with the extra input field last name added? Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

there are many errors here. you may find them by your own by debugging your console.log

  1. error, at let x = document.getElementsByName("first_name").[0]value;
  2. there are a to many }
  3. eventlisteners need to be on the input and shouldn't be inside the check function
  4. there are empty name attributes on your inputs

fixing it blind it would be something like:

let firstName = document.getElementsByName('first_name')[0];
let lastName = document.getElementsByName('last_name')[0];

function checkValid() {
  let x = firstName.value;
  let y = lastName.value;

  let text;
  text = '';

  if (x == '' || x == null) {
    text = 'Input not valid';
  }
  document.getElementById('first_name_errors').innerHTML = text;

  if (y == '' || y == null) {
    text = 'Input not valid';
  }
  document.getElementById('last_name_errors').innerHTML = text;
}

firstName.addEventListener('invalid', function () {
  firstName.focus();
});

lastName.addEventListener('invalid', function () {
  lastName.focus();
});
<input type="text" name="first_name"  placeholder="first name" class="input_fields" required>
<div class="error-message" id="first_name_errors"></div>
  
  <input class="save_btn" type="submit" value="Save" name="save_fname" onclick="checkValid()">

<br><br>

  <input type="text" name="last_name"  placeholder="last name" class="input_fields" required>
<div class="error-message" id="last_name_errors"></div>
  
  <input class="save_btn" type="submit" value="Save" name="save_lname" onclick="checkValid()">
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