Password Strength Checker using JavaScript Tutorial

In this tutorial, you will learn how to Create a Password Strength Checker using JavaScript. The tutorial aims to provide the IT/CS student and new programmers with a reference to enhance their JS knowledge and programming capabilities. Here, the step-by-step tutorial with snippets on how to create the Password Strength Checker is provided. The source code zip file is also provided and is free to download.

What is Password Strength?

Password Strength measures the effectiveness of a password for preventing attackers from easily guessing your password. The length, complexity, and unpredictable nature of a password all contribute to its strength.

How to Create a Password Strength Checker using JS?

In creating a Password Strength Checker using JavaScript, use the match method for checking the password characters. The match method retrieves the result from matching the string with the regular expression.

Steps for Creating a Password Strength Checker using JS?

  1. Create a Password Element on your page.
  2. In your JS Script, set variables for the minimum length of characters of upper case, lower case, digits, and symbol characters for both strong and medium password strength.
  3. Next, identify if the password value meets the minimum length of characters of either a strong or medium password. If not, password strength is weak.

Example

Here are the scripts of a simple Password Strength Checker using JavaScript.

Interface

The following snippet is an HTML file containing the elements script that displays the password input and the progress bar that displays the strength of the password entered. The script below loads the Bootstrap Framework using CDN. Save the file as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Password Strength Checker - JS</title>
  7. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha512-xh6O/CkQoPOWDdYTDqeRdPCVd1SpvCA9XXcUnZS2FmJNp1coAFzvtCN9BmamE+4aHK8yyUHUSCcJHgXloTyT2A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
  8. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
  9.  
  10. <script src="https://code.jquery.com/jquery-3.6.2.min.js" integrity="sha256-2krYZKh//PcchRtd+H+VyyQoZ/e3EcrkxhM8ycwASPA=" crossorigin="anonymous"></script>
  11. <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/js/all.min.js" integrity="sha512-naukR7I+Nk6gp7p5TMA4ycgfxaZBJ7MO5iC3Fp6ySQyKFHOGfpkSZkYVWV5R7u7cfAicxanwYQ5D1e17EfJcMA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  12. <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
  13.  
  14. html, body{
  15. height: 100%;
  16. width: 100%;
  17. }
  18. body{
  19. display: flex;
  20. height: 100%;
  21. width: 100%;
  22. flex-direction: column;
  23. }
  24. body>nav, body>footer{
  25. flex-shrink: 1;
  26. }
  27. body>main{
  28. flex-shrink: 1;
  29. flex-grow: 1;
  30. overflow: auto;
  31. margin: 1em 0;
  32. display: flex;
  33. align-items: center;
  34. }
  35. </style>
  36. </head>
  37. <body style="background:#EEF1FF">
  38. <nav class="navbar navbar-expand-lg navbar-dark" style="background:#7FBCD2">
  39. <div class="container">
  40. <a class="navbar-brand" href="./">Password Strength Checker - JS</a>
  41. <div>
  42. <a href="https://sourcecodester.com" class="text-light fw-bolder h6 text-decoration-none" target="_blank">SourceCodester</a>
  43. </div>
  44. </div>
  45. </nav>
  46.  
  47. <main class="container-fluid">
  48. <div class="col-lg-5 col-md-7 col-sm-12 col-xs-12 mx-auto">
  49. <h2 class="text-center">Check Password Strength using JavaScript</h2>
  50. <hr>
  51.  
  52. <div class="card mt-3 rounded-0">
  53. <div class="card-body rounded-0">
  54. <div class="container-fluid">
  55. <div class="mb-3">
  56. <label for="password" class="form-label">Password</label>
  57. <input type="text" class="form-control rounded-0" id="password">
  58. </div>
  59. <div class="mb-3">
  60. <div class="progress" id="password_strength">
  61. <div class="progress-bar" role="progressbar" aria-label="Password Strenght" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. </main>
  69. <footer class="container-fluid py-3" style="background:#7FBCD2; color:#fff">
  70. <div class="container-fluid my-2">
  71. <div class="text-center">
  72. <b>Password Strength Checker - JS &copy; 2022</b>
  73. </div>
  74. </div>
  75. </footer>
  76. <script src="password_strength_check.js"></script>
  77. </body>
  78. </html>

JavaScript

The following snippet is a JavaScript file that contains the script that checks the password strength. The file is known as password_strength_check.js and this file is loaded in the index.html.

  1. var password = document.getElementById('password')
  2. var strengthBar = document.getElementById('password_strength')
  3. /**
  4.   * Strong password values
  5.   */
  6. let strong_pass = {
  7. lowerCase: 8,
  8. upperCase: 3,
  9. digits: 2,
  10. symbols: 2,
  11. }
  12.  
  13. /**
  14.   * medium_password
  15.   */
  16. let medium_pass = {
  17. lowerCase: 5,
  18. upperCase: 1,
  19. digits: 1,
  20. symbols: 1,
  21. }
  22.  
  23. /**
  24.   * Patterns
  25.   */
  26. let lowerCase = /[a-z]/g;
  27. let upperCase = /[A-Z]/g;
  28. let digits = /[0-9]/g;
  29. let symbols = /[^a-zA-Z-0-9]/g;
  30.  
  31.  
  32. password.addEventListener('input', function(){
  33. let pass = this.value
  34. // Lower Case Characters length in Password
  35. var lc_len = pass.match(lowerCase) !== null ? pass.match(lowerCase).length : 0;
  36. // Upper Case Characters length in Password
  37. var uc_len = pass.match(upperCase) !== null ? pass.match(upperCase).length : 0;
  38. // Digit Characters length in Password
  39. var d_len = pass.match(digits) !== null ? pass.match(digits).length : 0;
  40. // Symbols length in Password
  41. var sym_len = pass.match(symbols) !== null ? pass.match(symbols).length : 0;
  42.  
  43.  
  44. // console.log(`lowerCase:`+lc_len)
  45. // console.log(`upperCase:`+uc_len)
  46. // console.log(`digits:`+d_len)
  47. // console.log(`symbols:`+sym_len)
  48.  
  49. var strength,strength_text, strengthClass;
  50.  
  51. if(lc_len >= strong_pass.lowerCase && uc_len >= strong_pass.upperCase && d_len >= strong_pass.digits && sym_len >= strong_pass.symbols){
  52. // Password is strong
  53. strength = 100;
  54. strength_text = "Password is Strong!!!";
  55. strengthClass = "bg-primary"
  56. }else if(lc_len >= medium_pass.lowerCase && uc_len >= medium_pass.upperCase && d_len >= medium_pass.digits && sym_len >= medium_pass.symbols){
  57. // Password is in Medium Strength
  58. strength = 66.66;
  59. strength_text = "Password is Good!!!";
  60. strengthClass = "bg-success"
  61. }else if(pass.length > 0){
  62. // Password is Weak
  63. strength = 33.33;
  64. strengthClass = "bg-danger"
  65. strength_text = "Password is Weak!!!";
  66. }else{
  67. // Password is null
  68. strength = 0;
  69. strengthClass = ""
  70. strength_text = ""
  71. }
  72.  
  73. //Progress Bar
  74. var strengthBar_progress = strengthBar.querySelector(".progress-bar")
  75.  
  76. // Remove Progress Bar Background color ClassName
  77. strengthBar_progress.classList.remove('bg-primary')
  78. strengthBar_progress.classList.remove('bg-success')
  79. strengthBar_progress.classList.remove('bg-danger')
  80.  
  81. // Add Progress Bar Background color ClassName if available
  82. if(strengthClass !== "")
  83. strengthBar_progress.classList.add(strengthClass);
  84.  
  85. // Set the Progress Bar Filled Width
  86. strengthBar_progress.style.width = strength + "%"
  87. // Set the Progress Bar Value Now Attribute
  88. strengthBar_progress.setAttribute('aria-valuenow', strength)
  89.  
  90. // Set the Progress Bar Text
  91. if(strength_text !== "")
  92. strengthBar.querySelector(".progress-bar").innerText = strength_text;
  93.  
  94. })
  95.  

There you go! You can now test the DEMO Password Strength Checker Application on your end.

Snapshots

Here are the following images of the result of the Password Strength Checker Application.

Weak Password

Password Strength Checker DEMO Application - JS

Medium Password

Password Strength Checker DEMO Application - JS

Strong Password

Password Strength Checker DEMO Application - JS

That's it! I have also provided the complete source code zip file on this website. You can download it by clicking the download button located below this article.

DEMO VIDEO

That's the end of this tutorial. I hope this Password Strength Checker using JavaScript Tutorial helps you with what you are looking for and you'll find this useful for current and future projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding :)

Add new comment