Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Setup Client side javascript validation

How to Setup Client side javascript validation ?

Aman Jain, September 16, 2021September 29, 2021

In this tutorial we will learn javascript validation without using any third party library. we will validate the simple form with 5 inputs fields.

Let’s start with simple steps

Step 1: Create a html file

Firstly we are going to create html file which contains a form with basic fields.

  1. first name
  2. last name
  3. email
  4. password

Index.html

<form name="registerform" onsubmit="return onSubmitHandle()">
              <div class="form-group">
                <label>First Name</label>
                <input
                  id="firstName"
                  type="text"
                  name="firstName"
                  class="form-control"
                />
              </div>
              <div class="form-group">
                <label>Last Name</label>
                <input
                  id="lastName"
                  type="text"
                  name="lastName"
                  class="form-control"
                />
              </div>
              <div class="form-group">
                <label>Email</label>
                <input
                  id="email"
                  type="text"
                  name="email"
                  class="form-control"
                />
              </div>
              <div class="form-group">
                <label>Password</label>
                <input
                  type="password"
                  name="password"
                  id="password"
                  class="form-control"
                />
              </div>
              <div class="form-group">
                <label>Confirm Password</label>
                <input
                  type="password"
                  name="confirmPassword"
                  class="form-control"
                />
              </div>
              <div class="form-group">
                <input type="submit" value="Submit" />
              </div>
            </form>

Step 2 : Create script file

We can use inline javascript or a separate javascript file. we will create 3 function.

  1. getElement : To find the input element in dom
  2. isEmpty : To check the input element is empty or not
  3. onSubmitHandler : To check the form validation on submit.

index.js

 function getElement(id) {
      return document.getElementById(id).value;
    }
    function isEmpty(id) {
      if (getElement(id) == '') {
        return true;
      }
      return false;
    }
    function ¸() {
      var isFormValid = true;
      if (isEmpty('firstName')) {
        isFormValid = false;
        alert('Please enter first name');
      } else if (isEmpty('lastName')) {
        isFormValid = false;
        alert('Please enter last name');
      } else if (isEmpty('email')) {
        isFormValid = false;
        alert('Please enter email');
      } else if (isEmpty('password')) {
        isFormValid = false;
        alert('Please enter password');
      }

      return isFormValid;
    }

Working live example and code

DOWNLOAD CODE

Related

Javascript

Post navigation

Previous post
Next post

Related Posts

How to generate component in angular cli?

August 29, 2021August 29, 2021

Angular cli has rich features of generating components, providers, modules and many more. Angular cli can also be used for build production ready product and serve the project for local environment. Usage of Angular cli ng generate command To generate the component ng generate component component_name To generate the modules…

Read More
Javascript Password and confirm password validation in angular

Password and confirm password validation in angular ?

September 11, 2021November 9, 2023

Angular provides built-in library for validation but it doesn’t have built in validation for Password and confirm password validation in angular. so in this tutorial i am going to explain how can we make custom validation for this same as angular built-in validations. Confirm password most useful utility when we…

Read More
Javascript Angular Dynamic Style CSS with Example

Angular Dynamic Style CSS with Example

October 5, 2022March 16, 2024

In this blog post, we’ll explore Angular dynamic style CSS with example in component’s styles based on certain conditions. Sometimes in our application we want dynamically change the color, size, font, background images so Angular provides various ways to conditionally apply styles to elements. In this post, we’ll explore three…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version