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

164
Views
html js mensaje de error no válido sigue apareciendo cuando la entrada es correcta

Tengo el siguiente campo de entrada con validación js para ingresar un nombre y cuando el campo se deja en blanco aparece un mensaje de error que es lo que necesitaba, sin embargo, cuando ingreso un nombre, acepta la entrada en mi formulario, sin embargo, aparece el mismo mensaje de error. brevemente.

 function myFunction() { let x = document.getElementsByName("first_name")[0].value; let text; text = ""; if (x == '' || x == null) { text = "Input not valid"; } document.getElementById("first_name_errors").innerHTML = text; } document.addEventListener('invalid', (function() { return function(e) { e.preventDefault(); document.getElementsByName("first_name").focus(); }; })(), true);
 <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()">

¿Cómo puedo arreglar esto?

Además, tengo la intención de usar esta plantilla para otros campos, como los campos de correo electrónico y contraseña, suponiendo que la parte principal de este código en la que debo centrarme en la adaptación es:

 let text; if (x == '' || x == null) { text = "Input not valid"; }

Se agradece mucho la ayuda/consejo. Gracias

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe tomar el primer elemento de NodeList devuelto por getElementsByName("first_name")

Como esto:

 let x = document.getElementsByName("first_name")[0].value;
about 4 years ago · Juan Pablo Isaza Report
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!