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

256
Visualizações
JQuery how to validate form inputs and textarea on whitespace and empty?

I am trying to validate multiple form elements such as <input> and <textarea> but I cant seem to get it to work. I tried getting the values from the form by putting the values in an array and looping through it but it doesn't work.

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 Respostas
Responde à pergunta

0

Your trim is wrong - it should be inputs[i].value.trim() === ""

Also you did not assign the validation to the form submit

$('#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 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