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 Angular KeyUp Event on Input Example

Angular KeyUp Event on Input Example

September 22, 2022March 16, 2024

Angular KeyUp Event on Input is an Angular event binding it triggers when user interacts with DOM like text based input element and when we key up the input. Input events in angular can be handled using input and output decorators and in this tutorial we will learn Angular KeyUp…

Read More

How to make a reactive form in angular?

August 29, 2021September 10, 2021

Angular has vast library of modules so reactive form is one of the part of angular library. Prerequisites knowledge: Angular HTML CSS Javascript So, let’s start building a from . Steps to create a reactive form in angular Create a new or existing angular project, you can also read this…

Read More
Javascript Remove hash from url in Angular

Remove hash from URL in angular 12 and deploy to server

February 1, 2022

From the first install angular come with hash URL routing and it’s defined in app-routing.module.ts. To remove hash from URL we need to change useHash key to false. Angular runs it development application ng server and whenever we wanted to deploy the application we need a server like Apache or…

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

  • 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 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
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version