The required attribute

required makes a field mandatory: if it is empty, the browser won't submit the form and shows a message next to the field. The check runs on a normal submit with a button or the Enter key, doesn't apply to every field type, and doesn't replace validation on the server.

Applies to

input, select, textarea

Values

A boolean attribute: the field is required as soon as the attribute is present. The value is ignored, so required="false" also turns the check on. To turn it off, remove the attribute.

Example

<form action="/order" method="post">
  <p>Fields marked * are required.</p>

  <p>
    <label for="name">Name *</label>
    <input id="name" name="name" type="text" required>
  </p>

  <!-- A first option with value="" means "nothing chosen" -->
  <p>
    <label for="delivery">Delivery *</label>
    <select id="delivery" name="delivery" required>
      <option value="">Choose a method</option>
      <option value="post">Post</option>
      <option value="courier">Courier</option>
    </select>
  </p>

  <!-- Shared name: choosing any one option is enough -->
  <fieldset>
    <legend>Payment *</legend>
    <label><input type="radio" name="payment" value="card" required> Card</label>
    <label><input type="radio" name="payment" value="cash" required> Cash</label>
  </fieldset>

  <!-- This box must be ticked or the form won't submit -->
  <p>
    <label><input type="checkbox" name="terms" required> I accept the terms *</label>
  </p>

  <!-- No required: this field can stay empty -->
  <p>
    <label for="comment">Comment</label>
    <textarea id="comment" name="comment"></textarea>
  </p>

  <button type="submit">Place order</button>
</form>

Edge cases

  • Works on input types text, search, url, tel, email, password, date, month, week, time, datetime-local, number, checkbox, radio and file, and on select and textarea.
  • Doesn't work on input types hidden, range, color, submit, reset, button and image: range and color always have a value, and nobody fills in a hidden field or a button.
  • Browsers skip fields with disabled (including inside a disabled fieldset) and input or textarea fields with readonly. select has no readonly attribute, so the check still applies there.
  • radio: buttons with the same name form a group. required on one of them is enough, and the user must pick one of the buttons, any of them. For clearer code, put required on every button. Without a name there is no group, and Chrome lets such a button pass with nothing chosen.
  • checkbox: required means this exact box must be ticked, as with accepting terms. A shared name doesn't group checkboxes, so every box with required becomes mandatory. HTML has no way to say "at least one of these"; that needs JavaScript.
  • select as a dropdown (no multiple, size not above 1): for the check to work, the first option needs an empty value, <option value="">Choose…</option>, and must not be inside an optgroup. An option with text but no value="" counts as a choice, so "Choose…" passes the check. So does value="0". A select with multiple or a size above 1 doesn't need an empty option: the user must pick at least one item.
  • novalidate on the form or formnovalidate on a submit button turns the check off, and the form goes through with empty fields. The second suits a button like "Save draft".
  • form.submit() in JavaScript sends the form without checking it. If you need the check, call form.requestSubmit() instead.
  • A button with type="button" or a button outside the form with no form attribute doesn't start a submit, so there is no check. A field outside the form isn't checked either until you give it a form attribute with that form's id.
  • required only checks that a field isn't empty. Spaces in text fields and textarea count as content. An email field discards them, so it stays empty.
  • type, pattern and minlength check format and length, but they let an empty field through. For a field that must be both filled in and correct, you need both kinds of attribute.
  • Form won't submit and shows nothing? Look for required on a field with display: none, visibility: hidden or the hidden attribute: it can't take focus, so Chrome shows no message on it and logs "An invalid form control … is not focusable" in the console. Make such a field disabled or remove required from it.
  • The browser only blocks at the moment of submitting, but :invalid applies straight away, so that style shows before anyone types. Use :user-invalid to show errors after interaction, and :required to mark mandatory fields.
  • For your own error text, call setCustomValidity in an invalid event handler and reset it to an empty string in an input handler; otherwise the filled field stays invalid. The title attribute doesn't replace the standard message.

Accessibility

  • Mark required fields with more than color: for example, an asterisk in the label and a note above the form saying what it means.
  • Give every field a linked label and wrap a radio group in a fieldset with a legend, so it's clear what the required field is asking.

Security

  • required only runs in the browser. A request can be sent without it: through form.submit(), by removing the attribute in DevTools, or directly with something like curl. So the server must check required fields and their format itself and reject bad data.
  • Trim spaces before checking on the server: required lets a string of only spaces through.

Related tools