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

257
Vistas
JQuery, ¿cómo validar las entradas de formulario y el área de texto en espacios en blanco y vacíos?

Estoy tratando de validar varios elementos de formulario, como <input> y <textarea> , pero parece que no puedo hacerlo funcionar. Intenté obtener los valores del formulario colocando los valores en una matriz y recorriéndolos, pero no funciona.

 if (!checkform($('#myform'))) { alert("Please fill all required fields"); } else { // do something } function checkform(form) { // get all the inputs within the submitted form var inputs = form.serializeArray(); for (var i = 0; i < inputs.length; i++) { // only validate the inputs that have the required attribute if (inputs[i].value.trim == null || inputs[i].value.trim === '') { // found an empty field that is required return false; } } return true; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <form id="myform"> <label class="form-label">Firstname</label> <input type="text" name="firstname" class="form-control" id="firstname" required> <label class="form-label">Message</label> <textarea id="message" type="text" rows="3" name="message" placeholder="enter your message"> </textarea> </form>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Su recorte es incorrecto: debería ser inputs[i].value.trim() === ""

Además, no asignó la validación al envío del formulario.

 $('#myform').on("submit",function(e) { if (checkform($(this))) return true; alert("Please fill all required fields"); e.preventDefault() }); function checkform($form) { // get all the inputs within the submitted form var inputs = $form.serializeArray(); for (var i = 0; i < inputs.length; i++) { // only validate the inputs that have the required attribute if (inputs[i].value.trim() === '') { // found an empty field that is required return false; } } return true; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <form id="myform"> <label class="form-label">Firstname</label> <input type="text" name="firstname" class="form-control" id="firstname" required> <label class="form-label">Message</label> <textarea id="message" type="text" rows="3" name="message" placeholder="enter your message"> </textarea> <input type="submit" /> </form>

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