WEB DEVELOPMENT • Beginner

HTML Forms

HTML forms are used to collect user input — login forms, registration forms, search bars, feedback forms, and more. They're the primary way users interact with websites. Mastering forms is essential for any web developer.

Basic Form Structure

<form action="/submit" method="POST">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" required>

    <button type="submit">Submit</button>
</form>

The <form> element wraps all inputs. Key attributes:

  • action — URL where form data is sent
  • method — GET (URL query) or POST (in request body)

The <input> Element

The most versatile form element. Its behavior depends on the type attribute.

Common Input Types

<input type="text" placeholder="Your name">
<input type="email" placeholder="you@example.com">
<input type="password" placeholder="Password">
<input type="number" min="1" max="100">
<input type="tel" placeholder="+91 9876543210">
<input type="date">
<input type="time">
<input type="url" placeholder="https://...">
<input type="color">
<input type="range" min="0" max="10">
<input type="checkbox">
<input type="radio" name="gender" value="male">
<input type="file" accept="image/*">
<input type="hidden" value="secret">

Labels — Critical for Accessibility

Every input should have a <label> linked via for attribute matching input's id.

<label for="email">Email Address</label>
<input type="email" id="email" name="email">

Or wrap the input inside the label:

<label>
    Email Address
    <input type="email" name="email">
</label>

Textarea — Multi-line Text

<label for="message">Message:</label>
<textarea id="message" name="message" rows="5" cols="40"></textarea>

Select Dropdown

<label for="course">Choose Course:</label>
<select id="course" name="course">
    <option value="">-- Select --</option>
    <option value="python">Python</option>
    <option value="fullstack">Full Stack</option>
    <option value="ai">AI & ML</option>
</select>

Checkbox & Radio Buttons

<!-- Checkboxes (multiple selection) -->
<label>
    <input type="checkbox" name="skills" value="html"> HTML
</label>
<label>
    <input type="checkbox" name="skills" value="css"> CSS
</label>

<!-- Radio buttons (single selection) -->
<label>
    <input type="radio" name="gender" value="male"> Male
</label>
<label>
    <input type="radio" name="gender" value="female"> Female
</label>
💡 Academy Tip

Radio buttons with the same name attribute work as a group — only one can be selected at a time. Checkboxes with the same name allow multiple selections.

Buttons

<button type="submit">Submit</button>
<button type="reset">Clear</button>
<button type="button" onclick="alert('Hi')">Click Me</button>

Form Validation

HTML5 provides built-in validation — no JavaScript needed for basic checks.

<input type="email" required>
<input type="text" minlength="3" maxlength="20">
<input type="number" min="18" max="100">
<input type="text" pattern="[A-Za-z]{3,}" title="Only letters, min 3">
  • required — field must be filled
  • minlength / maxlength — string length limits
  • min / max — number limits
  • pattern — regex validation

Full Registration Form Example

<form action="/register" method="POST">
    <label for="name">Full Name</label>
    <input type="text" id="name" name="name" required>

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

    <label for="password">Password</label>
    <input type="password" id="password" name="password"
           minlength="8" required>

    <label for="course">Course</label>
    <select id="course" name="course" required>
        <option value="">Select</option>
        <option value="python">Python</option>
        <option value="fullstack">Full Stack</option>
    </select>

    <label>
        <input type="checkbox" required> I agree to terms
    </label>

    <button type="submit">Register</button>
</form>

Fieldset & Legend

Group related fields together for better UX.

<fieldset>
    <legend>Personal Information</legend>
    <label>Name <input type="text"></label>
    <label>Email <input type="email"></label>
</fieldset>
📝 Try Yourself

Build a complete contact form with: name, email, phone, subject dropdown, message textarea, and a "Subscribe to newsletter" checkbox. Add required to name, email, and message. Test validation by submitting empty.

Quick Revision

  • Forms collect user input via <form>.
  • action = URL, method = GET/POST.
  • <input> type determines behavior.
  • Every input should have a <label>.
  • Multi-line text: <textarea>.
  • Dropdown: <select> + <option>.
  • Same-name radios = single choice.
  • Validation: required, minlength, pattern.

Interview Questions

Q: Difference between GET and POST methods?

A: GET appends form data to the URL (visible, limited size). POST sends data in the request body (hidden, no size limit). Use POST for sensitive data like passwords and forms that change server state.

Q: Why is the <label> element important?

A: Labels link text to inputs via the for attribute. Clicking the label focuses the input, and screen readers announce it. It's critical for accessibility and improves UX.

Q: How do you group radio buttons so only one can be selected?

A: Give all radios in the group the same name attribute. The browser then treats them as a single group.

Q: What is HTML5 form validation?

A: Built-in client-side validation using attributes like required, minlength, maxlength, pattern, type="email". It works without JavaScript, though JS provides more advanced validation.

Want to Master Web Development?

Join our Web Development course in Varanasi — practical classes and career support at our Sarnath centre.

Ask on WhatsApp
Chat with us