Course topics

By WebNest Studio Engineering Team

HTML Tutorial

HTML Form Validation

Modern HTML provides built-in, native form validation through attributes on input elements, letting browsers catch many common input errors before any JavaScript or server-side code runs. This is not a replacement for server-side validation — client-side checks can always be bypassed — but it provides fast, accessible feedback for legitimate users.

Why it matters

Catching a mistake before the form is sent saves the user a round trip and a re-typed form. HTML can require a field, limit its length and check its format with attributes alone. It is equally important to know the limit of this: anyone can bypass the browser, so the server must check everything again.

Core Validation Attributes

A handful of attributes cover most everyday validation needs without writing a single line of JavaScript.

  • required — the field must have a value before the form can be submitted.
  • pattern — the value must match a given regular expression (e.g. pattern="[0-9]{5}" for a 5-digit ZIP code).
  • min / max — set the minimum and maximum allowed values for number, date, and range inputs.
  • minlength / maxlength — constrain the number of characters allowed in text-based fields.

How the Browser Surfaces Errors

When a form is submitted with invalid fields, the browser blocks submission, focuses the first invalid field, and shows a small built-in tooltip describing the problem (e.g. "Please fill out this field"). Invalid fields also automatically match the CSS :invalid pseudo-class, which developers commonly use to add a red border or warning icon without any JavaScript.

Native Validation Is Not Enough Alone

Because all client-side validation, including HTML's built-in attributes, can be bypassed by disabling JavaScript, editing HTML through developer tools, or sending requests directly to the server, every value must also be validated and sanitized again on the server before it's trusted or stored. Native HTML validation exists to improve user experience with instant feedback, not to serve as a security boundary.

Example

A form field using several native validation attributes together

HTML
<label for="zip">ZIP Code</label>
<input
  type="text"
  id="zip"
  name="zip"
  required
  pattern="[0-9]{5}"
  maxlength="5"
  title="Enter a 5-digit ZIP code">

<label for="quantity">Quantity</label>
<input type="number" id="quantity" name="quantity" min="1" max="10" required>
Output
If the form is submitted with the ZIP field empty or not matching exactly 5 digits, the browser blocks submission and shows a validation message near that field; the quantity field similarly rejects values below 1 or above 10.

Common Mistakes

  • Relying only on client-side validation and skipping server-side validation entirely.
  • Writing an overly strict or incorrect pattern regular expression that rejects legitimately valid input.
  • Forgetting to add a helpful title attribute alongside pattern, leaving users with a generic, unhelpful error message.
  • Assuming required alone is sufficient for fields that also need format checking, like email or ZIP code fields.

Exercise

Add validation with attributes only. The username is required, between 3 and 15 characters, and may contain only lower-case letters and digits. The email is required. The quantity must be between 1 and 10.

Expected result: pressing Submit with an empty or invalid field shows the browser's own error message and does not submit.

HTML · starter code
<form>
  <p>
    <label for="username">Username</label>
    <input type="text" id="username" name="username">
  </p>
  <p>
    <label for="email">Email</label>
    <input type="email" id="email" name="email">
  </p>
  <p>
    <label for="quantity">Quantity</label>
    <input type="number" id="quantity" name="quantity">
  </p>
  <button type="submit">Submit</button>
</form>
Show hints
  • Use required, minlength, maxlength, min and max.
  • The allowed characters are set with pattern="[a-z0-9]+"; a title explains the rule in the error message.
Show solution
HTML · solution
<form>
  <p>
    <label for="username">Username</label>
    <input type="text" id="username" name="username" required minlength="3" maxlength="15"
           pattern="[a-z0-9]+" title="Lower-case letters and digits only">
  </p>
  <p>
    <label for="email">Email</label>
    <input type="email" id="email" name="email" required>
  </p>
  <p>
    <label for="quantity">Quantity</label>
    <input type="number" id="quantity" name="quantity" min="1" max="10">
  </p>
  <button type="submit">Submit</button>
</form>

Quick quiz

1. Which attribute makes a field mandatory?

  • A. mandatory
  • B. required
  • C. validate
  • D. notnull
Show answer

B. The form will not submit while a required field is empty.

2. Is browser validation enough to protect the server?

  • A. Yes, it cannot be bypassed
  • B. Yes, if pattern is used
  • C. No; requests can be sent without the browser, so the server must validate again
  • D. Only for email fields
Show answer

C. Client-side validation is for the user's convenience, not for security.

3. What does the novalidate attribute on a form do?

  • A. Disables the submit button
  • B. Makes every field required
  • C. Hides error messages only
  • D. Turns off the browser's built-in validation for that form
Show answer

D. It is used when validation is handled entirely by custom JavaScript.

Interview questions

What is the difference between client-side and server-side validation?

Show answer

Client-side validation runs in the browser, through HTML attributes or JavaScript, and gives the user immediate feedback without a network request. It can be bypassed completely, by editing the page in DevTools or by sending the request with another tool. Server-side validation runs on the server for every request and is the one that protects the data. A good form does both: the client for usability, the server for correctness and security.

Which HTML attributes provide validation without JavaScript?

Show answer

required for mandatory fields; minlength and maxlength for text length; min, max and step for numbers and dates; pattern for a regular expression the whole value must match; and the input type itself, such as email or url, which checks the format. The CSS pseudo-classes :valid and :invalid style fields according to the result.

Key Points to Remember

  • required, pattern, min/max, and minlength/maxlength provide validation without any JavaScript.
  • The browser blocks submission and shows a built-in message when validation fails.
  • Invalid fields match the :invalid CSS pseudo-class, useful for custom styling.
  • Client-side validation improves UX only — server-side validation is always still required.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering