Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Password and confirm password validation in javascript

Password and confirm password validation in javascript

Aman Jain, September 17, 2021November 21, 2021

In this tutorial, we will cover password strength validation , password not empty validation and confirm password validation. we will check on every submission of form whether the entered password pass or fail the criteria.

Password Strength validation in javascript

Password field must contain

  1. At least 8 characters
  2. Should be alphanumeric
  3. Should not be empty

index.html

<form name="registerform" onsubmit="return onSubmitHandle()">
              <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 id="error" style="color: red"></div>
              <div class="form-group">
                <input type="submit" value="Submit" />
              </div>
            </form>

Script for validation

function getElement(id) {
      return document.getElementById(id).value;
    }

    function onSubmitHandle() {
      var isFormValid = true;
      var pass = document.getElementById('password').value;
      var err_elmt = document.getElementById('error');

      if (pass == '') {
        err_elmt.innerHTML = 'Please enter password';
        isFormValid = false;
      } else if (pass.length < 8) {.  // check password is 8 char long
        err_elmt.innerHTML = 'Please must be at least 8 char long';
        isFormValid = false;
      } else if (!(/\d/.test(pass) && /[a-zA-Z]/.test(pass))) {. // check password contains atlease one char and digit
        err_elmt.innerHTML = 'Please enter at least 1 char and 1 number';
        isFormValid = false;
      } else {
        err_elmt.innerHTML = '';
      }

      return isFormValid;
    }

Here, we are using char length and regex to validate the string.

Check string contains atleast a character and one digit

!(/\d/.test("test1234") && /[a-zA-Z]/.test("Test1234"))

here, we are checking digit and string capital and lower case both.

Next , we are going to check password and confirm password. we will add new condition

else if (confirm_pass!=pass){ err_elmt.innerHTML = 'Password and confirm password does not match'; isFormValid = false; }

Screenshot

  • Screenshot 2021 09 17 at 12.09.54 AM
  • Screenshot 2021 09 17 at 12.10.04 AM
  • Screenshot 2021 09 17 at 12.10.15 AM
  • Screenshot 2021 09 17 at 12.10.36 AM
  • Screenshot 2021 09 17 at 12.10.45 AM
Javascript password confirm password validation

Related

Javascript

Post navigation

Previous post
Next post

Related Posts

Javascript Set focus on input angular example

Set focus on input angular example

September 25, 2022March 16, 2024

Angular is a great framework for building front-end web applications. One of its many features is the ability to set focus on input angular element. This can be useful in a variety of situations, such as when you want to focus on a particular input field after a user clicks…

Read More
Javascript How to set and get cookies in angular

How to set and get cookies in angular ?

April 14, 2022November 6, 2023

This article will give you the understanding of set and get cookies in angular. Cookies are used to store the data in client computer, Cookie can hold tiny information in computer and can retrieve when required for same website. Cookies can store key value in client system and its send…

Read More
Javascript custom validation in angular reactive form

How to make custom validation in angular reactive form ?

September 7, 2021October 1, 2021

Angular already have many built-in validators but sometimes we need some extra validation according to our project need. Steps to create custom validators Step 1: Create a function or class method where we can define our all custom validators. File: src/CustomValidators.ts 2. Define the function definition of custom validators to…

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

  • 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

  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version