The HTML required attribute
Quick answer
The HTML required attribute makes the control mandatory — the form cannot be submitted while it is empty. It is used on the <input>, <select> and <textarea> elements.
Overview
The required attribute marks a form control as mandatory. It is used on form controls such as <input>, <select> and <textarea>.
It is a form-control attribute: it configures how a control behaves, what it accepts, or its initial value, working alongside the control's <label> and parent <form>. Constraint attributes also feed the browser's built-in validation.
Syntax
<input type="text" required>
Values
| Value |
|---|
| A boolean attribute — present or absent. |
Example
<form onsubmit="event.preventDefault();this.nextElementSibling.textContent='Valid!'"><input required placeholder="Required" style="padding:8px;"> <button>Go</button></form><span></span>
Best practices
- Give every control a <label> so it has an accessible name.
- Treat client-side constraints as a convenience — always validate again on the server, since they can be bypassed.
- Choose the most specific input type so users get the right on-screen keyboard and built-in checks.
- Keep the submitted name and value meaningful for whatever processes the form.
Frequently asked questions
What does the required attribute do?
Marks a form control as mandatory.
Are HTML form attributes enough for validation?
They give instant feedback, but client-side checks can be bypassed, so always validate on the server too.
Do form controls still need a label?
Yes. Every control needs a <label> for an accessible name, whatever attributes you set.
Which elements use the required attribute?
It is an element-specific attribute, used on form controls such as <input>, <select> and <textarea>.