Skip to article content
FrontendXLesson 08 / 30

Input types, validation and constraint attributes

Build semantic, accessible and responsive interfaces from a blank page, and explain why each layout and styling decision works.

intermediate · about 35 minutes · 2 exercises

  • Explain
  • Implement

What you will be able to do

#
  • Apply native constraint validation attributes correctly.
  • Explain the difference between client-side and server-side validation.

Overview

#

HTML can validate before any JavaScript runs. The constraint attributes are required, minlength, maxlength, min, max, pattern and type. When a form is submitted, the browser checks these constraints and blocks submission with a native message if any fail. This validation is a convenience for users, never a security boundary, because it runs on the client and can be bypassed.

snippet.html
<form action="/signup" method="post">
  <label for="user">Username</label>
  <input id="user" name="user" type="text" required minlength="3" maxlength="20" pattern="[a-z0-9_]+" />
  <label for="age">Age</label>
  <input id="age" name="age" type="number" min="13" max="120" />
  <button>Create account</button>
</form>

The pattern attribute uses a full-string regular expression, so pattern="[a-z0-9_]+" requires the entire value to match. The type attribute also implies constraints: type="email" requires an email-shaped value, type="url" requires a URL shape, and type="number" restricts to numeric input.

Native validation messages are not customizable and their wording varies by browser and language, so many teams add their own messaging while keeping the native constraints as a first layer. Whatever you do, validate again on the server. A user can disable JavaScript, edit the DOM, or post directly to your endpoint. Client-side validation improves the experience; server-side validation protects the data.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Add native constraints to a username field so it is required, between 3 and 20 characters, and contains only lowercase letters, digits or underscores.

    starter.html
    <input id="user" name="user" type="text" />

    Reveal the reference answer
    reference.html
    <input id="user" name="user" type="text" required minlength="3" maxlength="20" pattern="[a-z0-9_]+" />

    Authored expected output: The input is required and rejects values shorter than 3, longer than 20, or containing characters outside the allowed set.

    Each attribute maps to a constraint the browser checks on submit. The pattern uses a full-string match.

  2. Exercise 2Explain

    Explain why a form that validates email on the client still needs server-side validation.

    Reveal the reference answer
    reference.text
    Client-side validation runs in the browser and can be bypassed by disabling JavaScript, editing the DOM, or sending a request directly to the endpoint. The server is the only place that can guarantee the data is valid and safe.

    Authored expected output: An explanation that names at least one bypass method and states that the server is the trust boundary.

    Validation on the client is a usability feature; validation on the server is a correctness and security requirement.

Common misconceptions

#
  • Client-side validation is enough to protect the server.
  • The pattern attribute matches a substring anywhere in the value.

Follow-up practice

#
  • html-css-09-form-submission

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.