HTML5 <input> required attribute but not inside <form>

Jon picture Jon · Jul 22, 2015 · Viewed 8.8k times · Source

The Required attribute works great inside the form Tags:

<form>
<input type="text" name="user" required>
</form>

But can I use the required attribute if I cannot wrap it as a form? Outside of the form this input required does not work for me:

<input type="text" name="user" required>

I can replicate it with JavaScript but Id like to know if outside form is possible

Answer

Foaster picture Foaster · Jul 22, 2015

The "required" attribute only works on form submit and since your input has no form the browser does not know what to validate on submit.

What the w3c says about "required": When present, it specifies that an input field must be filled out before submitting the form.

This would only be possible with JS like:

document.getElementById('your_input_id').validity.valid

Already discussed here 4 years ago:

html5 input type required without a form. Does it work?