HTML

HTML Form Validation Without JavaScript

HTML5 has built-in form validation: required fields, length and number limits, and format patterns, all without a single line of JavaScript.

RazakTerra
Reading time
1 min read
Comments
Discussion
HTML Form Validation Without JavaScript
On this page
  1. Required Fields
  2. Text Length Limits
  3. Number Limits
  4. Pattern (Custom Format)
  5. Email and URL Validation
  6. Common Validation Attributes
  7. Quick Tips

Required Fields

Playground
<input type="text" name="name" required />
Result
Cmd/Ctrl + Enter runs it · Esc leaves the editor

The browser will stop the form from submitting if this field is empty.

Text Length Limits

Playground
<input type="text" name="username" minlength="3" maxlength="20" required />
Result
Cmd/Ctrl + Enter runs it · Esc leaves the editor

Number Limits

Playground
<input type="number" name="age" min="18" max="99" required />
Result
Cmd/Ctrl + Enter runs it · Esc leaves the editor

Pattern (Custom Format)

Playground
<input type="text" name="zip" pattern="[0-9]{5}" placeholder="12345" required />
Result
Cmd/Ctrl + Enter runs it · Esc leaves the editor

This example only allows exactly 5 digits.

Email and URL Validation

Playground
<input type="email" name="email" required /> <input type="url" name="website" />
Result
Cmd/Ctrl + Enter runs it · Esc leaves the editor

The browser automatically checks if the format looks correct.

Common Validation Attributes

Attribute Purpose
required Field must be filled
minlength Minimum number of characters
maxlength Maximum number of characters
min Minimum number value
max Maximum number value
pattern Custom format using regular expression
type=“email” Checks for a valid email format
type=“url” Checks for a valid URL format

Quick Tips

  • Built-in validation improves user experience, but it’s not fully secure, always validate again on the server.
  • Clear labels help users understand what went wrong.
  • Keep pattern simple so it’s easy to maintain.
  • Test your form in different browsers, because validation messages can look slightly different.
  • With these HTML features, you can already make your forms much better before adding any JavaScript.

Written by

Razak

Writes about the web, slowly, and builds small tools on the side.

Discussion

Comments

Loading comments…

Keep reading