HTML required Attribute

HTML required Attribute: This attribute is defined as a boolean attribute. It specifies that the element must be filled out before submitting the form.

Elements: This attributes can be associated with three elements which are listed below.

<input>: The required attributes can be used in <input> elements.

HTML required Attribute

This attribute can be applied on <input>,<select>,<textarea> elements.

Syntax: <element required>

Browser Support

This attribute is supported by the following browsers:

  • Chrome
  • Firefox
  • Internet Explorer
  • Safari-does not support
  • Opera

Example: for <input> element

<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
  Username: <input type="text" name="usrname" required>
  <input type="submit">
</form>
</body>
</html>

Output:

html required attribute

 

Example: for <select> element

<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
<select required>
  <option value="">None</option>
  <option value="volvo">HTML</option>
  <option value="saab">CSS</option>
  <option value="mercedes">C</option>
  <option value="audi">C++</option>
</select>
<input type="submit">
</form>
</body>
</html>

Output:

HTML required attribute

Example: for <textarea> element

<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php">
<textarea rows="4" cols="50" name="comment" required>
</textarea>
<input type="submit">
</form>
</body>
</html>

Output:

HTML textarea element