Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

287
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda