Any attributes to ensure an HTML form can't be submitted?

635 views Asked by At

I want to server-render an HTML form in such a way that it is not submittable until it has been asynchronously enhanced by my JavaScript.

It looks like there's no disabled attribute for the form element (MDN).

I could add a disabled attribute to the submit button (and then later remove this with JavaScript when ready), but the user could still submit the form by focusing any input and pressing Enter.

Is there any way to prevent submission without JavaScript (short of just hiding the form entirely in the server-rendered HTML, and unhiding it with JS)?

3

There are 3 answers

3
Arsee On

use type="button" attribute to your submit button and then change it to type="submit"

1
connexo On

You can simply do

onsubmit="return false" 

on the form tag:

<form onsubmit="return false">
  <label>input
    <input type="text" name="input" name="a" />
  </label>
  <input type="submit" value="submit" />
</form>

0
callum On

Sorry, answering my own question - it turns out it's easy to make a form unsubmittable (in Chrome 69 at least) just by disabling the submit button.

When the only submit button is disabled, then even focusing a text field and pressing Enter does not submit the form.