HTML: input required
The required attribute tells the browser that an input must have a value before the form can be submitted.
What you will learn
- How to make an input mandatory
- How built-in browser validation behaves
- Why server-side validation is still necessary
Minimal example
<label for="name">Name</label>
<input id="name" name="name" required>
<button type="submit">Send</button>If the field is empty, the browser normally stops submission and asks the user to fill it in. The visible label explains what value is expected.
Required does not validate everything
required checks that a value is present. Use an appropriate type, pattern, minlength, or other constraint when the format also matters.
Common mistakes
- Do not use color alone to show that a field is required; provide text or a visible marker.
- Do not treat browser validation as a security boundary. Validate again on the server.
- Do not mark optional fields as required just to simplify processing.