MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Simple Form Validation0%
Study Materials
JavaScript for BeginnersProgress 0%
1.1What is JavaScript?1.2How JavaScript Works in Browser1.3Setting up Environment: VSCode, Live Server
12.1Interactive Button Click Counter12.2Simple Form Validation12.3Image Slider (Basic)12.4Final Mini Project: To-Do List Application
HomeTutorialsJavaScript for BeginnersSimple Form Validation
Image Slider (Basic)

Simple Form Validation

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

Form validation is one of the most critical responsibilities of frontend engineering. Validating data on the client side provides immediate feedback to users, prevents bad submissions, and enhances security before payloads reach backend servers.


1. Validation Requirements

In this hands-on project, we will validate a Student Registration Form:

  1. 1
    Full Name: Must not be empty, minimum 3 characters.
  2. 2
    Email Address: Must match a valid email format using regular expressions.
  3. 3
    Password: Must be at least 8 characters and contain at least one number.
  4. 4
    Confirm Password: Must match the Password field exactly.
  5. 5
    Real-time Feedback: Show red error borders and messages on invalid inputs; green borders on valid inputs.

2. The HTML Structure (index.html)

1<form id="signup-form" novalidate>
2 <h2>Student Registration</h2>
3 
4 <!-- Full Name -->
5 <div class="form-group">
6 <label for="username">Full Name</label>
7 <input type="text" id="username" placeholder="Sumit Sharma">
8 <small class="error-msg"></small>
9 </div>
10 
11 <!-- Email -->
12 <div class="form-group">
13 <label for="email">Email Address</label>
14 <input type="email" id="email" placeholder="sumit@example.com">
15 <small class="error-msg"></small>
16 </div>
17 
18 <!-- Password -->
19 <div class="form-group">
20 <label for="password">Password</label>
21 <input type="password" id="password" placeholder="At least 8 characters">
22 <small class="error-msg"></small>
23 </div>
24 
25 <!-- Confirm Password -->
26 <div class="form-group">
27 <label for="confirm-password">Confirm Password</label>
28 <input type="password" id="confirm-password" placeholder="Re-type password">
29 <small class="error-msg"></small>
30 </div>
31 
32 <button type="submit" id="submit-btn">Register Account</button>
33 <div id="success-alert" class="success-banner hidden">Registration Successful! 🎉</div>
34</form>
Note
[!NOTE] The novalidate attribute on the <form> disables default browser tooltips so our custom JavaScript UI can control all error styling.

3. The CSS Styling (style.css)

1.form-group {
2 margin-bottom: 1.25rem;
3 position: relative;
4}
5 
6input {
7 width: 100%;
8 padding: 0.75rem;
9 border: 2px solid #cbd5e1;
10 border-radius: 0.5rem;
11 outline: none;
12 transition: border-color 0.2s ease;
13}
14 
15/* Valid and Invalid State Classes */
16input.valid {
17 border-color: #10b981; /* Green */
18}
19 
20input.invalid {
21 border-color: #ef4444; /* Red */
22}
23 
24.error-msg {
25 color: #ef4444;
26 font-size: 0.75rem;
27 margin-top: 0.25rem;
28 display: block;
29}
30 
31.hidden { display: none; }
32.success-banner {
33 margin-top: 1rem;
34 padding: 0.75rem;
35 background: #dcfce7;
36 color: #15803d;
37 border-radius: 0.5rem;
38 text-align: center;
39}

4. The JavaScript Validation Engine (validator.js)

1const form = document.querySelector("#signup-form");
2const username = document.querySelector("#username");
3const email = document.querySelector("#email");
4const password = document.querySelector("#password");
5const confirmPassword = document.querySelector("#confirm-password");
6const successAlert = document.querySelector("#success-alert");
7 
8// Helper: Show Error
9function showError(input, message) {
10 input.classList.remove("valid");
11 input.classList.add("invalid");
12 const errorElement = input.parentElement.querySelector(".error-msg");
13 errorElement.textContent = message;
14}
15 
16// Helper: Show Success
17function showSuccess(input) {
18 input.classList.remove("invalid");
19 input.classList.add("valid");
20 const errorElement = input.parentElement.querySelector(".error-msg");
21 errorElement.textContent = "";
22}
23 
24// Validator 1: Name Check
25function checkUsername() {
26 const val = username.value.trim();
27 if (val.length < 3) {
28 showError(username, "Name must be at least 3 characters long");
29 return false;
30 }
31 showSuccess(username);
32 return true;
33}
34 
35// Validator 2: Email Regex Check
36function checkEmail() {
37 const val = email.value.trim();
38 const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
39 if (!emailRegex.test(val)) {
40 showError(email, "Please enter a valid email address");
41 return false;
42 }
43 showSuccess(email);
44 return true;
45}
46 
47// Validator 3: Password Strength
48function checkPassword() {
49 const val = password.value;
50 const hasNumber = /\d/.test(val);
51 if (val.length < 8 || !hasNumber) {
52 showError(password, "Password must be >= 8 chars and include at least 1 number");
53 return false;
54 }
55 showSuccess(password);
56 return true;
57}
58 
59// Validator 4: Password Confirmation
60function checkConfirmPassword() {
61 if (confirmPassword.value !== password.value || confirmPassword.value === "") {
62 showError(confirmPassword, "Passwords do not match");
63 return false;
64 }
65 showSuccess(confirmPassword);
66 return true;
67}
68 
69// Live Validation on Blur / Input
70username.addEventListener("blur", checkUsername);
71email.addEventListener("blur", checkEmail);
72password.addEventListener("input", checkPassword);
73confirmPassword.addEventListener("input", checkConfirmPassword);
74 
75// Form Submit Handler
76form.addEventListener("submit", (e) => {
77 e.preventDefault();
78 
79 const isNameValid = checkUsername();
80 const isEmailValid = checkEmail();
81 const isPasswordValid = checkPassword();
82 const isConfirmValid = checkConfirmPassword();
83 
84 if (isNameValid && isEmailValid && isPasswordValid && isConfirmValid) {
85 successAlert.classList.remove("hidden");
86 form.reset();
87 setTimeout(() => successAlert.classList.add("hidden"), 5000);
88 }
89});

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Why should you add novalidate to an HTML <form> when building custom JavaScript validation?
Question 2
What regular expression tests whether a string contains at least one numeric digit?
Question 3
Which event fires when an input element loses focus (e.g. user clicks outside the field)?
Question 4
Why is .trim() applied to text input values during validation?
Question 5
Can client-side JavaScript form validation completely replace backend validation on the server?
Next Lesson

Image Slider (Basic)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Interactive Button Click CounterImage Slider (Basic)

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext