TutreNews Learn

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 empty
  • min, max, step — for numbers and dates
  • minlength, maxlength — for text
  • pattern — a regular expression, e.g. a German postal code pattern="[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

  1. Guests: number between 1 and 8.
  2. Date: date picker, required.
  3. 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">