Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
MUI TextField: How to prevent form from being submitted?

I am building a form using React and MUI. I want to prevent the form from being submitted when the user presses the enter key. Normally I would do that with e.preventDefault(), but somehow that doesn't work. I think it has something to do with the TextField component from MUI.

Here is a simple example for my form and here is

function MyForm() {
  return (
    <form onSubmit={(e) => {
      e.preventDefault()
      /* do submit stuff here */
    }}>
      <TextField id="outlined-basic" label="Outlined" variant="outlined" />
      <Button type='submit'>Submit</Button>
    </form>
  );
}

Am I doing something wrong here?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

preventDefault function in onSubmit of form, prevents the page from being reloaded, and basically you need to write another preventDefault inside of the textfield as below:

<TextField
   onKeyPress={e => e.key === 'Enter' && e.preventDefault()}
/>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!