Tutorial

HTML forms: from input to inbox

A form is a conversation: the page asks, the visitor answers, and something on the other end listens. Here's how to build the asking part properly, and what has to exist on the listening end.

Anatomy of a form

Four pieces, each with a job:

<form action="../contact" method="post">
  <label for="email">Email address</label>
  <input type="email" id="email" name="email" required>
  <button type="submit">Send</button>
</form>
  • <form>: the container. action says where the data goes; method says how (post for sending data).
  • <label>: names the field. The for attribute must match the input's id.
  • <input>: the field itself. type sets its behavior; name labels the data when sent.
  • <button type="submit">: sends the form.

Use the right input type

The type attribute does more than change appearance: it changes the keyboard on phones and switches on free validation.

TypeUse forBonus
textNames, general textThe default
emailEmail addressesBrowser checks the format; phone shows @ key
telPhone numbersPhone shows numeric keypad
numberQuantities, agesUp/down controls; pairs with min/max
dateDatesNative date picker, no library needed
passwordPasswordsCharacters hidden as typed

Beyond inputs: <textarea> for multi-line messages, <select> for dropdowns, checkboxes for multiple choices, radios for one choice.

Labels are not optional

Every input needs a label connected with for and id. Two reasons, both non-negotiable: screen readers announce the label so blind users know what the field is, and clicking the label focuses the field, which makes small inputs easier to hit. Placeholder text is not a label; it disappears the moment someone starts typing.

Free validation, built into the browser

Before writing any JavaScript, HTML gives you validation for free. The browser checks these before the form can submit:

<input type="email" required>
<input type="number" min="13" max="120">
<input type="text" minlength="3" maxlength="50">

required blocks empty submissions; type="email" rejects malformed addresses; min, max, minlength, and pattern set the rules. The browser even shows the error messages.

What happens after submit

Here's the honest part most tutorials bury: HTML alone can't receive a form. The action attribute points at a server endpoint, a small program that accepts the data and does something with it: saves it, emails it, validates it again. Without that endpoint, the form goes nowhere.

And a warning that matters: browser validation is a courtesy, not security. Anyone can bypass it in seconds. Always validate again on the server, where the attacker can't reach the code.

Five rules of good forms

  1. Label everything. No exceptions, no placeholder-only fields.
  2. Ask for as little as possible. Every extra field lowers completion; if you don't need it, don't ask.
  3. Validate twice. Browser validation for instant feedback, server validation for actual safety.
  4. Confirm the submission. Show a success message so the visitor knows it worked; silence breeds duplicate submits.
  5. Expect spam. Any public form will be found by bots; add a honeypot field or CAPTCHA before you need it, not after.

Worked example: rebuilding a poor order form

The starting form asked for one field labelled Name, a plain text field for email, and a single box labelled Address. It used placeholder text in place of labels and gave no help when a field was wrong. Rebuild it field by field.

Split the name only where your records need it split. If you print delivery labels, a first name and last name as separate fields can help. If you only need to know who placed the order, keep one full name field and avoid forcing people to decide where a multi part name breaks. Either way, give the fields real labels and autocomplete values:

<label for="first-name">First name</label>
<input type="text" id="first-name" name="first-name"
  autocomplete="given-name" required>

<label for="last-name">Last name</label>
<input type="text" id="last-name" name="last-name"
  autocomplete="family-name" required>

Change the email field to the email type, so the browser checks the format and phones show a suitable keyboard. Add autocomplete="email" so browsers can fill it from saved details:

<label for="email">Email address</label>
<input type="email" id="email" name="email"
  autocomplete="email" required>

Replace the single address box with an address block. Separate lines let you print labels, check a postcode, and spot a missing street line before dispatch:

<label for="street">Street address</label>
<input type="text" id="street" name="street"
  autocomplete="street-address" required>

<label for="city">City</label>
<input type="text" id="city" name="city"
  autocomplete="address-level2" required>

<label for="postcode">Postcode</label>
<input type="text" id="postcode" name="postcode"
  autocomplete="postal-code" required>

Rewriting three error messages

An error message should name the field, say what is wrong, and say how to fix it.

  • Before: "Invalid input" on the email field. After: "Enter an email address in the form name@example.com. Your order confirmation will go to this address."
  • Before: "Invalid input" on the postcode field. After: "Enter your full postcode, for example SW1A 1AA. We use it to calculate delivery."
  • Before: "Invalid input" when the form was sent empty. After: "Two fields still need attention: email address and street address. The missing fields are marked above."

Keep the values the person already typed when you show an error. Making someone retype a whole form after one mistake is where orders get abandoned.

Frequently asked questions

Can I build a contact form with only HTML?

You can build how it looks and validates. Receiving the message needs a server endpoint or a form-handling service; HTML alone can't do that part.

What's the difference between GET and POST?

GET puts the data in the URL (fine for searches, terrible for passwords). POST sends it in the request body. Use POST for anything submitted.

Do I need JavaScript for forms?

Not for the basics: structure, labels, and validation are all HTML. JavaScript adds nicer experiences like live feedback and submission without page reload.

Last verified: October 23, 2026, against MDN Web Docs.

Need forms that actually deliver?

We build forms with validation, spam protection, and reliable delivery.

Get a quote →

← All tutorials