Home > Software engineering >  Required attribute in Html
Required attribute in Html

Time:06-27

can I use required attribute here? when I am not using form tag ? whether required works only inside form tag?

<label for="fname">First name:</label>
<input type="text" id="fname" name="fname" required> 

CodePudding user response:

No, as mentioned here:

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.

Here is also what W3C says:

When present, it specifies that an input field must be filled out before submitting the form.

So — assuming you aren't intending to use some sort of JS solution — no form, no required.

  • Related