Masatoshi Nishiguchi
Masatoshi Nishiguchi's Blog

Masatoshi Nishiguchi's Blog

React disable submit event triggered by pressing enter key

Masatoshi Nishiguchi's photo
Masatoshi Nishiguchi
·Nov 25, 2017·

1 min read

Sometimes I want to disable the submit event triggered by pressing the enter key while focusing an input field.

<form
  action="/users/sign_up"
  onSubmit={handleSubmit}
  onKeyPress={event => {
    if (event.which === 13 /* Enter */) {
      event.preventDefault();
    }
  }}
  acceptCharset="UTF-8"
  method="post"
>

Resources

 
Share this