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

The required attribute works fine 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 is not suitable for me:

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

I can reproduce it using JavaScript, but I'd like to know if an external form is possible

+4
source share
1 answer

The required attribute only works in the submit form, and since your entry has no form, the browser does not know what to check for sending.

What w3c says about "required": When it is present, it indicates that the input field must be filled in before submitting the form .

This is only possible with JS:

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

4 :

html5 . ?

+6

Source: https://habr.com/ru/post/1599127/


All Articles