JavaScript Notes
Learn to build an email validator in JavaScript using regex, real-time feedback, common domain suggestions, and bulk validation. A great regex and DOM project.
What You'll Learn
- How to write and understand an email validation regex
- How to give real-time feedback as the user types
- How to break the regex into parts so you know what each part does
- How to suggest common domain corrections (gmial.com → gmail.com)
- How to validate a list of emails in bulk
Project Overview
An email validator checks whether a string follows the standard email format user@domain.tld. It provides instant visual feedback and can also catch common typos in domain names.
Component Structure
How It Works — Flow Diagram
Step-by-Step Build
Step 1 — HTML Structure
<div class="validator-container">
<h1>Email Validator</h1>
<div class="input-group">
<input type="text" id="emailInput" placeholder="Enter email address..." autocomplete="off">
<button id="checkBtn" class="btn-check">Check</button>
</div>
<div id="feedback" class="feedback"></div>
<div id="suggestion" class="suggestion"></div>
<hr>
<h2>Bulk Validator</h2>
<textarea id="bulkInput" rows="6" placeholder="Paste emails here, one per line..."></textarea>
<button id="bulkBtn" class="btn-bulk">Validate All</button>
<ul id="bulkResults" class="bulk-results"></ul>
</div>Step 2 — The Email Regex (Explained)
Step 3 — Real-Time Feedback
Step 4 — Error Reason Helper
Step 5 — Domain Typo Suggestions
Step 6 — Bulk Validator
Console Output
validateEmail("john@gmail.com") → true ✅
validateEmail("john@gmial.com") → true ✅ (but suggests gmail.com)
validateEmail("johngmail.com") → false ❌ Missing @ symbol
validateEmail("john@") → false ❌ Missing domain after @
validateEmail("john@g.c") → false ❌ Domain extension too short
validateEmail("john+filter@sub.io")→ true ✅ (+ and sub-domains supported)Edge Cases to Handle
| Input | Expected Result |
|---|---|
" john@gmail.com " | trim() clears whitespace → valid |
john@gmail.com. | Trailing dot → invalid |
a@b.co | Short but valid TLD → valid |
"@gmail.com" | Missing username → invalid |
john@@gmail.com | Double @ → invalid |
Challenge Yourself
- MX record check — use a public DNS API to verify the domain actually has mail records
- Disposable email detection — block known temp-mail domains
- CSV import — let users upload a
.csvfile and validate the email column - Copy valid emails — add a "Copy Valid" button to clipboard
- Visual stats — show a donut chart: % valid vs invalid from bulk validation
Best Practices
- Use
trim()andtoLowerCase()before every validation check - Don't over-restrict the local part —
+,.,%are valid in real email addresses - The regex is good enough for UI validation; for critical cases use a backend check
- Show specific error reasons rather than a generic "invalid email" message
- Levenshtein distance is great for small domain suggestion lists
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Build a JavaScript Email Validator - Step by Step Project Tutorial.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, mini, projects, email
Related JavaScript Master Course Topics