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

190
Visualizações
React/TS prevent form submission on enter while using "required" tag

There are already a bunch of helpful answers on preventing form submit on enter (example1, example2) However none of these work if I want to include the "required" tag on any of the inputs. See code below:

    
    const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
        event.preventDefault();
        // do other stuff
    };
    
    return (
            <form id="LocationForm" onSubmit={handleSubmit} >
                <input type="text" placeholder="Please add country" onKeyUp={handleCountryChange} />
                <input type="text" placeholder="Please add city" onKeyUp={handleCityChange} required />
                <input type="submit" value="Submit" />
            </form>
    )

When I press enter in the first input, a dialog pops up that says "This field is required". Ideally, I only want this to pop up when I click the submit button and not when I press enter in any other input. Of course I could implement this logic myself without using the required tags, but would rather avoid re-inventing the wheel here... unless there is some other standard practice that people use. Any pointers?

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

0

Try adding onKeyPress={(e) => e.preventDefault() to the <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