๐Ÿ“… Week 7 - Day 3: Form Validation with JavaScript

Friday | Duration: 3 hours

๐ŸŽฏ Learning Objectives

๐Ÿ” Why Form Validation?

Form validation ensures users enter correct data before submitting. This improves user experience and data quality by catching errors before they reach the server.

โœ… Common Validation Rules

๐Ÿ” Basic Validation Examples

1. Check if Field is Empty

function validateName() { let name = document.getElementById("name").value; if (name === "" || name === null) { alert("Name is required!"); return false; } return true; }

2. Email Validation

function validateEmail() { let email = document.getElementById("email").value; // Check if email contains @ and . if (!email.includes("@") || !email.includes(".")) { alert("Please enter a valid email!"); return false; } return true; } // Better email validation with regex function isValidEmail(email) { let emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailPattern.test(email); }

3. Password Length Validation

function validatePassword() { let password = document.getElementById("password").value; if (password.length < 8) { alert("Password must be at least 8 characters!"); return false; } return true; }

๐ŸŽจ Complete Form Validation Example

Form Validation

Registration Form

๐Ÿงช Live Demo Form

Try the Validation

๐Ÿ“‹ Validation Checklist

โœ๏ธ Practice Exercises

  1. Create a login form with email and password validation
  2. Add age validation (must be 18 or older)
  3. Validate username (no spaces, min 5 characters)
  4. Create password strength indicator (weak/medium/strong)
  5. Build a contact form with all validation rules

โœ… Today's Deliverable

โ† Previous Course Home Next: Week 8 โ†’