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

143
Visualizações
Controlled form: display a div if invalid input

For a training, I'am trying to check if the input value of this code base includes a "@" when onBlur:

<form>
  <input
    type='text'
    name='my_input'
    value={inputValue}
    onChange={(e) => setInputValue(e.target.value)}
    onBlur={(e) => checkEmail(e.target.value)}
    ></input>
  <button>Save</button>
</form>

Therefore I added a function to display a <div> below the form (or at least below the input field) if there is no "@" in the input, but without success: 🧐

function checkEmail(value) {
  if (!value.includes('@')) {
    <div>⚠️ Warning, there is no @, this is not a valid email address.</div>;
  }
}

Knowing console.log(!value.includes('@')) well return true. 😫

Any clue on how to get the <div>⚠️ Warning, there is no @, this is not a valid email address.</div> shown below the form or input when invalid input?

Thanks in advance. 🙂

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

0

You are returning your JSX in the checkEmail function, which isn't quite right. What you want to do instead is have a flag that indicates whether the warning JSX should be rendered, and setup a logic to do so if it is true. inputValue being a state is really handy in this case, one small && operator should handle the rendering function.

<form> ... </form>

{ inputValue && !inputValue.includes('@') && 
(<div>⚠️ Warning, there is no @, this is not a valid email address.</div>) }

PS: you can get rid of onBlur={(e) => checkEmail(e.target.value)} and its handler method.

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