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

226
Visualizações
can't modify style of default error messages in forms in React

I have a form in a React function component and I want to change the styles(color, background color) for the error messages that appear when the required attribute is set to true for a field. I have a const formRef = React.useRef(); and I use it to check if I can go further like this:

if (formRef.current.reportValidity()) {
      // do stuff
    }

enter image description here

Any suggestions?

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

0

As far as I know, you can not style this default validation error from html. What I did so far was to disable native validation, and listen to input's change, and in case of error update some state. For example

const [inputValue, setInputValue] = useState<string>('');
const [errorMessage, setErrorMessage] = useState<string>(''):
const MIN_LENGTH = 5;

/* update input value */
const handleInputChange = (event) => {
   const value = event.target.value;
    
   /* clear error on change */
   if (errorMessage !== '') {
     setErrorMessage('');
   }
}

/* set error if needed on blur */
const handleInputBlur = () => {
   if (inputValue.length <= MIN_LENGTH) {
      setErrorMessage(`Username should have at least ${MIN_LENGTH} characters`
   }
}

return (
<div>
   <input onChange={handleInputChange} onBlur={handleInputBlur} 
          value={inputValue} />
   {errorMessage !== '' && <p>{errorMessage}</p>
</div>
)

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