Back to roadmaps html Course
Table of Contents (31 guides)

HTML5 Form Validation: Built-in Browser Checking

Historically, verifying that users filled out forms correctly required complex JavaScript. Modern HTML5 simplifies this by providing built-in validation attributes. The browser automatically tests these rules before sending any data to the server.


1. Essential Validation Attributes

You can attach the following validation attributes directly to your input controls:

A. The required Attribute

Prevents form submission if the field is left empty.

<input type="text" name="first_name" required>

B. Length Limits: minlength and maxlength

Defines the minimum or maximum character count.

<input type="password" name="password" minlength="8" maxlength="20" required>

C. Value Limits: min and max

Defines the numerical bounds for type="number" or type="date".

<input type="number" name="age" min="18" max="120">

D. The pattern Attribute (Regular Expressions)

Forces the user to match a specific format using a regular expression pattern.

<!-- Matches exactly 5 digits (US ZIP code) -->
<input type="text" name="zipcode" pattern="[0-9]{5}" placeholder="e.g. 90210" title="Please enter a 5-digit ZIP code.">

2. A Comprehensive Validation Example

Here is a form demonstrating all these validation concepts together:

<form action="/register" method="POST">
  <div>
    <label for="username">Username (minimum 5 letters):</label>
    <input type="text" id="username" name="username" minlength="5" required>
  </div>

  <div>
    <label for="email">Valid Email Address:</label>
    <input type="email" id="email" name="email" required>
  </div>

  <div>
    <label for="invite">Invite Code (exact pattern 'ABC-1234'):</label>
    <input type="text" id="invite" name="invite_code" pattern="[A-Z]{3}-[0-9]{4}" placeholder="ABC-1234" required>
  </div>

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

3. Disabling Validation with novalidate

If you are developing custom JavaScript validations and want to bypass the browser's default popups, you can add the novalidate attribute to the parent <form> tag.

<!-- Browser will submit the form even if required fields are blank -->
<form action="/login" method="POST" novalidate>
  ...
</form>

4. Key Takeaways

  • Client-side vs. Server-side: HTML5 validation improves UX by providing instant feedback. However, never trust client-side validation alone. Destructive users can easily bypass browser checks. Always revalidate all submitted data on the server side!
Published on