JavaScript Form Validation

Data Validation in JavaScript

Data validation is the process of checking whether data entered by a user or received from an external source satisfies predefined rules before it is processed or stored. JavaScript can perform client-side validation to provide immediate feedback to users, while server-side validation should also be used to protect application data and maintain security.

Beginner 30 Minutes Monopoly IT Solutions Updated 2026-08-24
JavaScript class training banner
5.0
5.0
4.6
Overview

What you'll cover

Learning objectives

  • Understand data validation
  • Understand client-side and server-side validation
  • Validate required form fields
  • Validate username length
  • Validate password length
  • Validate password complexity
  • Display validation error messages
  • Use JavaScript DOM methods for validation
  • Validate a form during submission
  • Use regular expressions for pattern validation

Prerequisites

  • HTML Forms
  • Variables
  • Functions
  • Conditional Statements
  • DOM Basics
  • Basic JavaScript

Related topics

  • HTML Forms
  • DOM Manipulation
  • Regular Expressions
  • Event Handling
  • String Methods
  • Client-Side Validation
  • Server-Side Validation
  • Form Submission
Theory

Concept breakdown

What is Data Validation?

Data validation is the process of ensuring that data entered by users or received from external sources meets predefined rules before it is processed or stored.

Why is Data Validation Important?

Validation helps prevent invalid data, user input errors, inconsistencies, and certain security problems. It ensures that application data follows the expected format and rules.

Client-Side Validation

Client-side validation is performed in the user's browser. It provides immediate feedback without requiring a request to the server.

Server-Side Validation

Server-side validation is performed on the server after the data is submitted. It is essential because client-side validation can be bypassed by users.

Required Field Validation

Required field validation checks whether a form field contains a value. For example, username and password fields should not be empty.

Length Validation

Length validation checks whether the number of characters in a value satisfies a minimum or maximum length requirement.

Pattern Validation

Pattern validation checks whether a value follows a particular format. Regular expressions can be used to validate complex patterns such as password requirements.

Form Submission Validation

The onsubmit event can be used to validate form data before the form is submitted to the server. Returning false prevents submission when validation fails.

Syntax
Hands-on example

Login Form Data Validation

Console Output
Walkthrough

Step by step

  1. 1

    Create the Form

    Create an HTML form containing username and password input fields.

  2. 2

    Create Validation Functions

    Create validateUserName() and validatePassword() functions to validate individual form fields.

  3. 3

    Get Input Controls

    Use document.getElementById() to retrieve the username and password input elements.

  4. 4

    Validate Required Fields

    Check whether username and password contain empty values.

  5. 5

    Validate Username Length

    Use the length property to ensure that the username contains at least 8 characters.

  6. 6

    Validate Password Length

    Check whether the password contains at least 6 characters.

  7. 7

    Validate Password Pattern

    Use regular expressions to check for uppercase letters, lowercase letters, numbers, and special characters.

  8. 8

    Display Error Messages

    Use innerHTML to display validation messages inside the small elements.

  9. 9

    Validate During Input

    The oninput event calls the validation function whenever the user changes the input value.

  10. 10

    Validate on Blur

    The onblur event validates the field when the user moves away from the input control.

  11. 11

    Validate Form Submission

    The onsubmit event calls both validation functions before submitting the form.

  12. 12

    Prevent Invalid Submission

    Returning false from a validation function prevents the form from being submitted.

Summary

Key takeaways

Key points

  • Data validation ensures that input follows predefined rules.
  • Client-side validation provides immediate feedback to users.
  • Server-side validation is still required for security and data integrity.
  • Required field validation checks whether a value is empty.
  • The length property can be used for length validation.
  • Regular expressions can be used for pattern validation.
  • The oninput event can perform validation while the user types.
  • The onblur event can perform validation when an input loses focus.
  • The onsubmit event can validate the entire form before submission.
  • Returning false from onsubmit prevents form submission.
  • Validation functions should return true when the input is valid and false when it is invalid.

Advantages

  • Provides immediate feedback to users
  • Reduces invalid form submissions
  • Improves user experience
  • Reduces unnecessary server requests
  • Helps maintain consistent input formats
  • Can be implemented using simple JavaScript functions
  • Regular expressions support complex validation patterns

Disadvantages

  • Client-side validation can be bypassed.
  • It depends on JavaScript being enabled in the browser.
  • Complex validation rules can make client-side code difficult to maintain.
  • Client-side validation alone is not sufficient for application security.
  • Server-side validation is still required.
Real-world use

User Registration System

A registration form can validate username, email, password, mobile number, age, and other details before submitting the information to the server. Client-side validation provides immediate feedback, while server-side validation verifies and protects the submitted data.

Interview prep

Frequently asked questions

Data validation is the process of checking whether input data satisfies predefined rules before it is processed or stored.

Client-side validation is validation performed in the user's browser before data is sent to the server.

Client-side validation can be bypassed, so server-side validation is required to protect data and maintain application security and integrity.

The length property can be used to determine the number of characters in a string.

The onsubmit event is used to execute validation or other logic when a form is submitted.

Returning false from the form's onsubmit handler prevents the form from being submitted.

A regular expression is used to check whether a string matches a specific pattern.

The oninput event occurs whenever the value of an input element changes.

The onblur event occurs when an input element loses focus.

Test yourself

Quick MCQ practice

1. What is the main purpose of data validation?

2. Which property returns the number of characters in a string?

3. Which event occurs when a form is submitted?

4. Which event is useful for validating input while the user types?

5. Which method tests whether a string matches a regular expression?

6. Why should server-side validation also be performed?

Apply it

Practice on your own

Coding exercise

Registration Form Validation

Create a registration form containing username, email, password, confirm password, age, and mobile number fields. Validate required fields, username length, email format, password complexity, password confirmation, age range, and mobile number format using JavaScript.

Assignment

  1. Create a Student Registration form.
  2. Add Student Name, Email, Mobile Number, Age, Password, and Confirm Password fields.
  3. Validate that all required fields are entered.
  4. Validate that Student Name contains minimum 3 characters.
  5. Validate the email format using a regular expression.
  6. Validate the mobile number using a regular expression.
  7. Validate that age is between 18 and 60.
  8. Validate that the password contains uppercase, lowercase, number, and special character.
  9. Validate that Password and Confirm Password are identical.
  10. Display appropriate error messages next to each field.
  11. Prevent form submission when any validation fails.

Tags

#javascript#data-validation#form-validation#client-side-validation#regular-expression#dom#beginner

getintouch

Book for Live Demo!