References

Beginner-friendly references for web development, with live, editable examples.

The HTML required attribute

Attribute All modern browsers
In short

The HTML required attribute makes a form field mandatory, as in <input name="email" required>. If the field is empty when the form is submitted, the browser stops the submission and reports the problem on that field. A required one-row <select> also needs an empty first option.

Overview

The HTML required attribute marks a form control that must have a value before its form is submitted. Even required="false" makes a field required, since it's a boolean attribute. <select> and <textarea> accept it, as does every <input> type except range, color, hidden and buttons.

What counts as filled depends on the control. A text field needs at least one character, and a space counts. A checkbox has to be checked, a radio group needs one selected button, a file input needs a file, and a select needs an option other than its placeholder.

The placeholder is a first <option> with an empty value, as in <option value="">Choose a size</option>. Without one, a select that shows one row starts on its first option and passes untouched, so the HTML standard requires the placeholder there.

The check runs when someone submits the form by button or Enter key, or a script calls requestSubmit(), unless novalidate or formnovalidate turns it off. The browser then fires an invalid event at each failing field instead of the submit event, and Chrome and Firefox focus the first one.

Syntax

<label for="email">Email</label>
<input type="email" id="email" name="email" required>
<label for="size">Size</label>
<select id="size" name="size" required>
  <option value="">Choose a size</option>
  <option>Small</option>
  <option>Large</option>
</select>

Values

Value
Needs no value to make the control mandatory.

Example

Live example
<form id="rsvp">
  <p style="margin:8px 0"><label for="guest">Name (required)</label>
  <input id="guest" name="guest" required></p>
  <p style="margin:8px 0"><label for="meal">Meal (required)</label>
  <select id="meal" name="meal" required>
    <option value="">Choose a meal</option>
    <option>Chicken</option>
    <option>Vegetarian</option>
  </select></p>
  <p style="margin:8px 0"><label><input type="checkbox" name="terms" required> I accept the terms (required)</label></p>
  <button>Send RSVP</button>
  <p id="msg" role="status" style="overflow-wrap:anywhere"></p>
  <p>Press Send RSVP, then fill it in and resend.</p>
</form>
<script>
const form = document.getElementById('rsvp');
const msg = document.getElementById('msg');
form.querySelector('button').addEventListener('click', function () {
  const empty = [...form.elements].filter(el => el.validity.valueMissing).map(el => el.name);
  msg.textContent = empty.length ? 'Still empty: ' + empty.join(', ') : '';
});
form.addEventListener('submit', function (e) {
  e.preventDefault();
  msg.textContent = 'Sent: ' + [...new FormData(form)].map(pair => pair.join('=')).join(', ');
});
</script>

Best practices

  • Mark every radio button in a group with required. One is enough to make the group required, but marking them all leaves no doubt for anyone reading the markup.
  • Style errors with :user-invalid rather than :invalid. An empty required field matches :invalid at page load. :user-invalid waits for a submit attempt or a finished change, such as leaving an edited text field or unchecking a box.
  • Send forms from scripts with requestSubmit(), not submit(). The submit() method skips constraint validation, so empty required fields go through.
  • When a VAT number only matters for business orders, set vat.required = business.checked from a change listener. Setting the property to false also removes the attribute.

Accessibility

When a form mixes required and optional fields, mark the required ones visibly. The marker shouldn't rely on color alone, and text or an icon is the usual choice. The attribute itself maps to aria-required for assistive technology, so no extra ARIA is needed.

Frequently asked questions

Does required check that an email address is valid?
No, required only rejects an empty field, while type=email checks the format of a value that's there. Use both, since the email check alone lets an empty field through.
How does required work on a select?
A one-row required select fails while its placeholder, a first option with an empty value, is selected. An empty option inside an optgroup doesn't count as a placeholder, so put it directly inside the select.
Can required make someone check at least one box in a checkbox group?
No, required applies to each checkbox on its own, so only the boxes that carry it must be checked. Radio buttons differ, since required on one of them makes the whole group need a selection.
Why does a field with only spaces pass required?
Spaces in a text field count as content, so required sees a value and passes it. Tel fields and textareas behave the same way, while email and URL fields still turn down spaces alone.
How do I change the message for a required field?
Call setCustomValidity() with your own text when the required check fails. Clear it with an empty string once the field has a value, or the custom message keeps the field invalid.