Forms
Input types and validation
Goal: Use input types and built-in validation attributes · 9 min
The type of an input changes its keyboard on phones and its built-in checks: email, tel, url, number, date, password, checkbox, radio, search.
Validation attributes let the browser check input before sending:
required— must not be emptymin,max,step— for numbers and datesminlength,maxlength— for textpattern— a regular expression, e.g. a German postal codepattern="[0-9]{5}"
Use autocomplete values (email, postal-code, name) so browsers can fill forms. Important: client-side validation is for convenience; the server must validate again, because requests can be sent without the browser.
Worked example: Booking form fields
- Guests: number between 1 and 8.
- Date: date picker, required.
- Postal code: exactly five digits.
<label for="g">Guests</label>
<input id="g" name="guests" type="number" min="1" max="8" required>
<label for="d">Date</label>
<input id="d" name="date" type="date" required>
<label for="z">Postal code</label>
<input id="z" name="zip" pattern="[0-9]{5}" autocomplete="postal-code">