Back to Tutorials
JavaScript
20 min read
Sahasransu Satpathy
3/5/2026

Form Validation in JavaScript

Learn how to validate form inputs using JavaScript with real-time feedback and error handling

Introduction

Form validation is essential to ensure that users submit correct and complete data. In this tutorial, you will learn how to implement client-side form validation using JavaScript with real-time feedback.


Step 1: HTML Form Setup

Create a basic form:

<form id="contactForm">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name" required />
  
  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required />
  
  <label for="password">Password:</label>
  <input type="password" id="password" name="password" required minlength="6" />
  
  <button type="submit">Submit</button>
</form>
<div id="errorMessages"></div>

Step 2: Basic JavaScript Validation

Add JavaScript to check for empty fields and valid input:

const form = document.getElementById('contactForm');
const errorMessages = document.getElementById('errorMessages');

form.addEventListener('submit', function(e) {
  e.preventDefault();
  errorMessages.innerHTML = '';

  const name = form.name.value.trim();
  const email = form.email.value.trim();
  const password = form.password.value.trim();
  let errors = [];

  if(name === '') errors.push('Name is required');
  if(email === '') errors.push('Email is required');
  if(password.length < 6) errors.push('Password must be at least 6 characters');

  if(errors.length > 0) {
    errorMessages.innerHTML = errors.map(err => `<p style="color:red;">${err}</p>`).join('');
  } else {
    alert('Form submitted successfully!');
    form.reset();
  }
});

Step 3: Real-Time Validation

Validate fields as the user types:

form.name.addEventListener('input', () => {
  if(form.name.value.trim() === '') {
    form.name.style.borderColor = 'red';
  } else {
    form.name.style.borderColor = 'green';
  }
});

form.email.addEventListener('input', () => {
  const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  form.email.style.borderColor = regex.test(form.email.value) ? 'green' : 'red';
});

Step 4: Advanced Validation

  • Add password strength check
  • Validate phone numbers or custom formats
  • Highlight error messages near each input

Step 5: Mini Project Example

  • Create a Registration Form with: name, email, password, confirm password, phone number
  • Implement real-time validation and submit prevention for invalid input
  • Style errors using CSS

Conclusion

Client-side form validation improves user experience and reduces server load. Combining HTML5 validation attributes with JavaScript allows flexible and interactive forms.


SEO Suggestions:

  • Main keywords: JavaScript form validation, client-side form validation, validate form inputs JS, HTML JS form tutorial
  • Meta description: Learn how to implement client-side form validation using JavaScript. Real-time feedback, error handling, and examples included.
  • Catchy title suggestions: "Form Validation in JavaScript – Step-by-Step Guide", "Master Form Validation with JavaScript"

Previous Tutorial

Browse All Tutorials