0

I am building a form using React, Typescript and Formik. The form has three inputs. I want to prevent the form from submitting when the user presses enter in the input fields.

My first approach was using the event.preventDefault() statement, but that didn't work. Then I found this solution here on stackoverflow, but in this solution the Formik component is used and not the useFormik hook.

So I wounder how to disable form submit on enter with useFormik?

3 Answers 3

1

The useFormik hook provides a handleSubmit methods which handles that, pass it to your form onSubmit prop.

...
const fomrik = useFormik(...);

return (
  <form onSubmit={formik.handleSubmit}>
...
Sign up to request clarification or add additional context in comments.

Comments

1
<button type='button'>Submit</button>

type='button'

By using this, pressing enter would not trigger the form onSubmit .

Comments

1

we can prevent it on the form:

<Form onKeyDown={(e) => {
    if (e.key === 'Enter') {
      e.preventDefault();
    }
  }}
>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.