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

10,075

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?

Share:
10,075
Jon
Author by

Jon

Updated on July 03, 2022

Comments

  • Jon
    Jon almost 2 years

    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