What is a required attribute?

The required attribute marks a form field that must be filled in: the browser refuses to submit the form while that field is empty.

It is a one-word attribute you add to an input, select or textarea. When the visitor presses submit, the browser checks each required field. If one is empty, it stops the submit, moves to that field and shows a short message. A required checkbox must be checked, and in a group of radio buttons with the same name one must be chosen.

It is the cheapest validation there is: no JavaScript, it works with screen readers, and the message is in the visitor's language. It only checks that something was entered. A single space in a text field passes, and whether an email address looks valid is checked by type="email", not by required.

The check runs only in the browser. A bot that posts straight to the endpoint, or a form with the novalidate attribute, skips it. So treat required as a help for visitors and not as a guarantee about the data that arrives.

html
<label>Email <input type="email" name="email" required></label>
<label><input type="checkbox" name="consent" required> I agree to be contacted</label>

Related