📝 The 10KB JavaScript Library You Don’t Need
We used to load jQuery + jQuery Validate (45KB) just to validate emails. Now? Zero JavaScript. Native HTML does it all.
The Power of Native Validation
🎯 All The Validation You Need (No JS)
<form>
<!-- Email validation -->
<input type="email" required
placeholder="Email">
<!-- URL validation -->
<input type="url" required
placeholder="Website">
<!-- Number with min/max -->
<input type="number"
min="18" max="120"
required
placeholder="Age">
<!-- Pattern matching (regex) -->
<input type="text"
pattern="[A-Za-z]{3,}"
title="At least 3 letters"
required
placeholder="Name">
<!-- Length constraints -->
<input type="password"
minlength="8"
maxlength="50"
required
placeholder="Password">
<!-- Phone with pattern -->
<input type="tel"
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
placeholder="123-456-7890"
required>
<button type="submit">Submit</button>
</form>
/* That's it! Browser validates everything automatically */
/* Shows native error messages in user's language */
/* Blocks submit if invalid */
/* Zero JavaScript needed */
📱 Mobile Benefits
- type=”email” → @ keyboard
- type=”tel” → number pad
- type=”url” → .com key
- type=”number” → numeric keyboard
Better UX without any code!
🎨 Custom Error Messages
<input type="email"
required
oninvalid="this.setCustomValidity('Please enter a valid email')"
oninput="this.setCustomValidity('')">
Or use :invalid CSS pseudo-class for styling!
💅 Styling Validation States
/* Valid inputs */
input:valid {
border: 2px solid #2ecc71;
background: #d4edda;
}
/* Invalid inputs (after user interaction) */
input:invalid:not(:placeholder-shown) {
border: 2px solid #e74c3c;
background: #f8d7da;
}
/* Required inputs */
input:required {
border-left: 4px solid #3498db;
}
/* Optional inputs */
input:optional {
border-left: 4px solid #95a5a6;
}
/* In range (for type="number") */
input:in-range {
border-color: #2ecc71;
}
/* Out of range */
input:out-of-range {
border-color: #e74c3c;
}
| Validation Type | HTML Attribute | Example |
|---|---|---|
| Required field | required |
<input required> |
| Email format | type="email" |
<input type=”email”> |
| Min/Max value | min max |
<input min=”0″ max=”100″> |
| Length limits | minlength maxlength |
<input minlength=”3″> |
| Pattern (regex) | pattern |
<input pattern=”[0-9]+”> |
🚀 Advanced: Form Validation API
const form = document.querySelector('form');
const email = document.querySelector('#email');
// Check validity programmatically
if (email.validity.valid) {
console.log('Email is valid!');
}
// Check specific validation states
email.validity.valueMissing // true if required and empty
email.validity.typeMismatch // true if wrong type
email.validity.patternMismatch // true if pattern fails
email.validity.tooLong // true if exceeds maxlength
email.validity.rangeOverflow // true if > max
// Get validation message
console.log(email.validationMessage);
// Prevent default validation UI
form.noValidate = true;
form.addEventListener('submit', (e) => {
e.preventDefault();
// Your custom validation UI
});
⚠️ When You Still Need JavaScript
- Complex cross-field validation (e.g., “password confirmation must match”)
- Async validation (e.g., “check if username is taken”)
- Custom error styling (native tooltips look dated)
- Progressive enhancement (add extra validation on top)
But start with native validation. Add JS only when needed!
“We removed jQuery Validate (45KB) and replaced it with… nothing. Native HTML validation works better, looks better on mobile, and costs 0 bytes.”
📊 The Bottom Line
0 KB
JavaScript
95%
Browser Support
∞
Use Cases
