Skip to main content

Lesson 5 of 9

HTML5 Form Validation

HTML5 provides built-in form validation that works WITHOUT JavaScript. Attributes like required, minlength, maxlength, min, max, pattern, and type-based validation (email, url, number) let the browser check inputs before submission. CSS pseudo-classes :valid and :invalid let you style validated fields with green/red borders.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Validation Attributes

  • required - Field must be filled before submit. Browser shows 'Please fill out this field' tooltip.
  • minlength / maxlength - Minimum/maximum text length. Example: minlength='8' for passwords.
  • min / max - Minimum/maximum value for numbers, dates, ranges. Example: min='18' max='120' for age.
  • pattern='regex' - Custom regex validation. Example: pattern='[0-9]{10}' for 10-digit phone numbers.
  • type-based validation - type='email' requires @ symbol. type='url' requires protocol. type='number' requires digits only.
  • step - Controls valid increments. step='0.01' for currency (allows $9.99). step='5' for ranges in multiples of 5.
  • title='hint text' - Custom hint shown in the validation error tooltip alongside the browser's default message.
  • novalidate on <form> - Disables ALL validation. Used when you want to handle validation with JavaScript instead.
  • formnovalidate on <button> - Disables validation for just that submit button (e.g., 'Save Draft' button).

Validation Example

Validation Example
<!DOCTYPE html>
<html>
<head>
  <style>
    input:valid { border:2px solid #229c6b; }
    input:invalid { border:2px solid #e74c3c; }
    input:focus:invalid { outline:none; box-shadow:0 0 0 3px rgba(231,76,60,0.3); }
    input:focus:valid { outline:none; box-shadow:0 0 0 3px rgba(4,170,109,0.3); }
    input { padding:10px; border-radius:6px; width:100%; max-width:300px; margin:6px 0; display:block; box-sizing:border-box; }
    label { font-weight:600; margin-top:12px; display:block; }
  </style>
</head>
<body>
  <h2>Registration Form (HTML5 Validation)</h2>
  <form>
    <label>Full Name (2-50 chars)</label>
    <input type="text" required minlength="2" maxlength="50" placeholder="John Doe">
    
    <label>Email</label>
    <input type="email" required placeholder="you@example.com">
    
    <label>Phone (10 digits)</label>
    <input type="tel" pattern="[0-9]{10}" title="Enter 10-digit phone number" placeholder="9876543210">
    
    <label>Age (18-120)</label>
    <input type="number" min="18" max="120" required>
    
    <label>Password (8+ chars)</label>
    <input type="password" required minlength="8" placeholder="Min 8 characters">
    
    <button type="submit" style="margin-top:16px;padding:10px 24px;background:#229c6b;color:white;border:none;border-radius:6px;cursor:pointer">Register</button>
  </form>
</body>
</html>
Diagram
Loading diagram…

HTML5 validates inputs client-side without requiring custom JavaScript

Quick Quiz: Validation

Key Takeaways

  • HTML5 provides built-in form validation that works WITHOUT JavaScript.
  • required - Field must be filled before submit. Browser shows 'Please fill out this field' tooltip.
  • minlength / maxlength - Minimum/maximum text length. Example: minlength='8' for passwords.
  • min / max - Minimum/maximum value for numbers, dates, ranges. Example: min='18' max='120' for age.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/9 done