Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
HTML

HTML5 Forms: The Built-In Validation That Replaced jQuery Validate

- 23.02.26 | 23.02.26 - ErcanOPAK

📝 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.”

— Frontend Lead, SaaS Startup

📊 The Bottom Line

0 KB
JavaScript
95%
Browser Support
∞
Use Cases

Related posts:

HTML: Use Inputmode to Show the Right Mobile Keyboard

HTML: Use Inputmode to Show the Right Mobile Keyboard

HTML: Draw with Canvas API

HTML: Draw with Canvas API

HTML5: Fix a Native dialog Element's Backdrop Not Covering the Full Viewport Because a Transformed A...

HTML5: Fix a Native dialog Element's Backdrop Not Covering the Full Viewport Because a Transformed A...

Views: 15

Post navigation

CSS Grid + Subgrid: The Layout Technique That Killed My Need for Flexbox
JavaScript Performance: The One Weird Trick That Made Our App 10x Faster

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com