Skip to content

Client-Side Form Validation

Medium ~35 min

What you'll build

A signup form that validates email format, password length/strength, and password confirmation as the user types, showing inline error messages.

Goals

  • Use HTML5 attributes (required, type, minlength) for built-in validation
  • Write custom validators for email pattern and password match
  • Show inline error messages on invalid fields
  • Disable the submit button until all fields are valid

Prerequisites

  • HTML form elements
  • CSS for styling error states
  • JavaScript input event listeners

Steps

  1. 1

    Build the form HTML

    Create a form with email, password, and confirm-password fields. Each field has a label, an input, and an error message span.

    Code
    <form id="signup">
      <div class="field">
        <label for="email">Email</label>
        <input type="email" id="email" name="email" required>
        <span class="error" id="email-error"></span>
      </div>
      <div class="field">
        <label for="password">Password</label>
        <input type="password" id="password" name="password" required minlength="8">
        <span class="error" id="password-error"></span>
      </div>
      <div class="field">
        <label for="confirm">Confirm Password</label>
        <input type="password" id="confirm" name="confirm" required>
        <span class="error" id="confirm-error"></span>
      </div>
      <button type="submit" id="submit" disabled>Sign Up</button>
    </form>

    Expected result

    A form with three fields and a disabled submit button renders.

  2. 2

    Write the email validator

    Create a function that checks the email against a regex pattern. If invalid, show a message in the error span and add an .invalid class to the input.

    Code
    function validateEmail(input) {
      const error = document.getElementById('email-error');
      const pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      if (!pattern.test(input.value)) {
        error.textContent = 'Please enter a valid email address';
        input.classList.add('invalid');
        return false;
      }
      error.textContent = '';
      input.classList.remove('invalid');
      return true;
    }

    Expected result

    Typing 'abc' shows an error; typing '[email protected]' clears it.

  3. 3

    Write the password validators

    Validate password length (≥8) and that confirm matches password. Show appropriate messages for each.

  4. 4

    Wire input listeners and toggle submit

    Add 'input' event listeners to all fields that run the validators and toggle the submit button's disabled state based on whether all validators pass.

  5. 5

    Style the error states

    Add CSS: .invalid inputs get a red border, .error spans are red and small. Test the full flow by typing invalid then valid input.

    Code
    .field { margin-bottom: 1rem; }
    input.invalid { border: 2px solid #dc2626; }
    .error { color: #dc2626; font-size: 0.85rem; display: block; min-height: 1em; }
    button:disabled { opacity: 0.5; cursor: not-allowed; }

    Expected result

    Invalid fields show red borders and messages; submit enables only when all valid.

Try it yourself

What you learned

You built real-time validation combining HTML5 constraints with custom JavaScript — the pattern used by nearly every production form before server-side validation.

Related Resources

Related Lessons

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic