HTML Forms and Native Validation

Build forms with the right input types, labels and built-in validation before reaching for any JavaScript library.

Beginner · about 4 hours · difficulty 2/5

Learn first: Semantic and Accessible HTML

Forms are how users talk back to your app: sign-up, search, checkout, settings. The browser already provides a lot of behaviour; learn it first so libraries later feel like enhancements rather than magic.

Building blocks

  • <form action method> wraps controls; submitting sends name=value pairs. In SPAs you usually intercept submit with JavaScript, but the semantics still matter (Enter submits, password managers work).
  • Input types such as email, tel, url, number, date, checkbox, radio, file give mobile users the right keyboard and built-in checks.
  • <select>, <textarea>, <fieldset> with <legend> for grouped radios or checkboxes.
  • Every control needs a visible <label for>; placeholders are hints, not labels.

Native validation

Attributes like required, minlength, maxlength, pattern, min, max and type="email" validate without code. The :invalid and :user-invalid pseudo-classes let you style errors, and the Constraint Validation API (checkValidity(), setCustomValidity()) lets JavaScript hook in.

UX that respects users

  • Use autocomplete values (email, current-password, one-time-code, postal-code) so autofill works.
  • Show errors next to the field, in text, not only in red.
  • Never rely on client-side validation for security: the server must validate again.

Move on when you can build an accessible multi-field form (with a radio group and a file input) that validates natively and submits its data to a test endpoint.

Key concepts

  • form, action and method
  • Input types
  • Labels, fieldset and legend
  • Built-in validation attributes
  • Constraint Validation API
  • autocomplete tokens
  • :invalid and :user-invalid styling

Mini project

Build a job application form with text, email, phone, a radio group, a resume file upload and a consent checkbox, using only native validation and styled error states.

Check yourself

Why is a placeholder not an acceptable replacement for a label?

Free resources