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

184
Visualizações
How to check several input's value real time?

Description:

I have three inputs: First name, Last name, and age. I am looking for a solution if users type all inputs then show validated, and if one of them is empty or user deletes one of them showing invalid.

I try the following lines:

$('#name' && '#lname' && '#age').keyup(function () {
   $('#display').removeClass('invalides');
   $('#display').replaceWith('Valid');
   $('#display').addClass('validated');
});
.invalides {
  border: 1px solid red;
}
.validated {
  border: 1px solid green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="name" />
<input type="text" id="lname" />
<input type="number" id="age" />

<span id="display" class="invalides">Invalid</span>

You may find it in JSFiddle

Why do I use keyup? In the following question, they offers keyup: Real time updating of values on a form

Problem description:

  1. It still shows validated after deleting the value of inputs.
  2. it is not real-time checking.
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should check each input value on input event.

const nameEl = $('#name');
const lnameEl = $('#lname');
const ageEl = $('#age');

$('#name, #lname, #age').on('input', () => {
   // if nothing is empty
   if(nameEl.val().length && lnameEl.val().length && ageEl.val().length) {
    $('#display').removeClass('invalides').addClass('validated').html('Valid');
   }
   else $('#display').addClass('invalides').removeClass('validated').html('Invalid');
});
.invalides {
  border: 1px solid red;
}
.validated {
  border: 1px solid green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="name" />
<input type="text" id="lname" />
<input type="number" id="age" />

<span id="display" class="invalides">Invalid</span>

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