freeCodeCamp/guide/english/html/attributes/required/index.md

1.2 KiB

title
Required

Required

The HTML required attribute specifies that filling out an input field is mandatory before a form can be submitted.
If the user does not fill in the input field, the form will not submit and it will give a message asking the user to fill out the field.

The required attribute is applicable to <input>, <select>, and <textarea>.

<input> example:

<!DOCTYPE html>
<html>
  <head>
    <title>HTML Required Attribute</title>
  </head>
  <body>
    <form action="/">
      Text Field: <input type="text" name="textfield" required>
      <input type="submit" value="Submit">
    </form>
  </body>
</html>

<select> Example:

<form action="/action.php">
<select required>
  <option value="">None</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
</form>

<textarea> Example:

<form action="/action.php">
  <textarea name="comment" required></textarea>
  <input type="submit">
</form>

More Information:

MDN article on the input element