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

201
Visualizações
Showing a red alert text near an input if the input does not match what we want

So basically I want to display some red text in a website if the content of an input is not what I want the person to put. My input is a number and I want to check that the number is > 15.

I know how to check for that with jquery, but to show the red text I was thinking of having it already in the html with a hidden class and then removing the hidden class when the input is not correct, but it seems a bit dirty ? Is there a better way to make some text appear in that case ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation

Without knowing what kind of input we're talking about exactly, and with no code provided, it's hard to give an exact answer for your issue. But the pattern attribute on forms should do the trick, when it comes to client-side validation. You can use CSS to customize your current input field, no reason to use jQuery or hidden objects.

From Mozilla:

input:invalid {
  border: 2px dashed red;
}

input:invalid:required {
  background-image: linear-gradient(to right, pink, lightgreen);
}

input:valid {
  border: 2px solid black;
}
<form>
  <label for="choose">Would you prefer a banana or a cherry?</label>
  <input id="choose" name="i_like" required pattern="[Bb]anana|[Cc]herry">
  <button>Submit</button>
</form>

The pattern attributes uses regular expressions, so you should be able to make it validate all kinds of inputs.

EDIT:

https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation#using_built-in_form_validation

min and max: Specifies the minimum and maximum values of numerical input types

input:invalid {
  border: 2px dashed red;
}

input:invalid:required {
  background-image: linear-gradient(to right, pink, lightgreen);
}

input:valid {
  border: 2px solid black;
}
<input type="number" id="choose" name="i_like" min="16">

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