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 Customdirective in angular

How to Create Custom Directive in Angular 12?

October 12, 2021February 8, 2024

In this tutorial we will learn to create the custom directive in angular and how to use them in our project. Custom directives are those directives by which we can enhance the html capabilities and can bind custom attributes, methods to it. We can reuse them in any element or…

Read More
Javascript Laravel Ajax Autocomplete Using Select2

Laravel Customized Autocomplete Options Using Select2

July 18, 2022July 18, 2022

In this article we will learn to use Laravel Customized Autocomplete Options Using Select2. Select2 is useful when we want live search of bulk data or to convert the existing select boz with multi features like search, multi select and options customizations. Autocomplete search is mostly work of javascript and…

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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version