Web Development · Module 2 · Lesson 41 min read

Forms & Inputs

Forms collect user input

HTML
<form action="/signup" method="post">
  <label for="email">Email</label>
  <input id="email" name="email" type="email" required>
  <button type="submit">Sign up</button>
</form>
  • action is where data is sent; method is get or post.
  • name is the key the server receives.
  • Always connect a <label> to each input with for and id. Tapping the label focuses the field, and screen readers announce it.

Input types (the right one gives the right phone keyboard)

TypeUse
text, email, password, tel, urltext fields
number, rangenumbers
date, timepickers
checkbox, radiochoices
fileuploads
Plus <textarea> for long text and <select> with <option> for dropdowns.

Built-in validation

required, minlength, maxlength, min, max, pattern="[0-9]{10}", and type="email". The browser blocks submission and shows a message.

⚠️ Browser validation is for user experience only. Always validate again on the server, because anyone can bypass the browser.

Useful attributes

placeholder (a hint, never a replacement for a label), autocomplete="email", disabled, readonly, value.

Example

HTML
<form onsubmit="event.preventDefault(); alert('Thanks, ' + this.name.value + '!')">
  <p>
    <label for="name">Name</label><br />
    <input id="name" name="name" required minlength="2" autocomplete="name" />
  </p>
  <p>
    <label for="email">Email</label><br />
    <input id="email" name="email" type="email" required autocomplete="email" />
  </p>
  <p>
    <label for="level">Level</label><br />
    <select id="level" name="level">
      <option>Beginner</option>
      <option>Intermediate</option>
    </select>
  </p>
  <p>
    <label><input type="checkbox" name="news" /> Send me updates</label>
  </p>
  <button type="submit">Join</button>
</form>