Dofactory.com
Dofactory.com

HTML required Attribute

The required attribute on an input element specifies that data must be provided before the form can be submitted.

Elements that accept this attribute are <input>, <textarea>, and <select>.

Example

#

Two <input> element with required attributes.
The text fields must have a value before the form can be submitted.






<form action="/tutorial/action.html">
  <label for="firstname">*First name</label><br/>
  <input type="text" id="firstname" name="firstname" required><br />
  <label for="lastname">*Last name</label><br/>
  <input type="text" id="lastname" name="lastname" required><br /><br />

  <input type="submit">
</form>

For additional details see our HTML input required Reference.


Using required

The required attribute specifies that an input element must have a value before submitting the form.

This attribute is part of the built-in validation functionality in HTML.

On an <input> element, the required attribute only applies to these types:


Syntax

<tagname required>

Elements that accept required

The following elements accept the required attribute.

Elements Description
<input> Specifies an input field -- see example above
<textarea> Specifies a multi-line text input field.
<select> Creates a dropdown field.

<textarea> with required

A <textarea> tag with a required attribute.
The textarea must have a value (i.e. text) before the form can be submitted.




<form action="/tutorial/action.html">
  <label for="message">*Message</label><br/>
  <textarea required name="message" rows="5" cols="55"></textarea>

  <br /><br />
  <input type="submit">
</form>

For additional details see our HTML textarea required Reference.


<select> with required

A <select> tag with a required attribute.
The dropdown must have a value selected before the form can be submitted.


<form action="/tutorial/action.html">
  <select name="size" required>
    <option value="">-- Select Size --</option>
    <option value="small">Small</option>
    <option value="medium">Medium</option>
    <option value="large">Large</option>
  </select><br />

  <button type="submit">Submit</button>
</form>

For additional details see our HTML select required Reference.


Browser support

Here is when required support started for each browser:

Chrome
5.0 May 2010
Firefox
4.0 Mar 2011
IE/Edge
10.0 Sep 2012
Opera
9.6 Oct 2008
Safari
10.1 Mar 2017

You may also like




Last updated on Sep 30, 2023




Guides