Forms and User Input
Build accessible forms with labels, inputs, validation, controls, and submission behavior.
Form Fundamentals
The <form> element represents an interactive section for submitting information. Its action specifies where data is sent and method determines the HTTP method used for submission. Common methods include GET and POST.
<form action="/search" method="get">\n <label for="query">Search</label>\n <input id="query" name="q" type="search">\n <button type="submit">Search</button>\n</form>- The name attribute determines the submitted field name.
- GET commonly represents retrieval-oriented submissions while POST is commonly used for creating or changing data.
Which attribute identifies the destination URL of a form submission?
Labels and Input Controls
Every user-facing form control should have an accessible label. A <label> can be associated with an input through matching for and id attributes, or by wrapping the input.
Placeholder text is a hint, not a replacement for an accessible label.
<label for="email">Email address</label>\n<input id="email" name="email" type="email" autocomplete="email">- Associate labels with their controls.
- Do not rely on placeholder text as the only label.
Which label attribute associates a label with an input id?
HTML Input Types
HTML provides specialized input types such as email, password, number, date, url, tel, search, checkbox, radio, and file. Using the appropriate type improves native validation, mobile keyboards, and accessibility.
<input type="email" name="email">\n<input type="password" name="password">\n<input type="date" name="birthday">\n<input type="number" name="age" min="1" max="120">\n<input type="file" name="resume" accept=".pdf">- Choose input types according to the expected data.
- Correct input types improve user experience and native validation.
Which input type is intended for email addresses?
Native Form Validation
HTML provides native validation attributes including required, minlength, maxlength, min, max, pattern, and type-specific constraints. Native validation improves basic client-side feedback but must not replace server-side validation.
Attackers can bypass browser validation. Always validate and sanitize untrusted input on the server.
<form>\n <label for="username">Username</label>\n <input id="username" name="username" required minlength="3" maxlength="20">\n\n <label for="age">Age</label>\n <input id="age" name="age" type="number" min="18" max="100" required>\n\n <button>Submit</button>\n</form>- Native validation improves user experience.
- Server-side validation is still required for security and data integrity.
Which attribute makes an input mandatory?