How to Create a Password Validator in JavaScript

How to Create a Password Validator in JavaScript

Introduction

In this tutorial we will create a How to Create a Password Validator in JavaScript. This tutorial purpose is to validate the password you been entered. This will cover all the basic function that will validate your password in the textbox. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system that needed to have a stronger password to avoid getting hack.. I will give my best to provide you the easiest way of creating this program Password Validator. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display button and textboxes. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">How to Create a Password Validator in JavaScript</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <form>
  18. <div class="form-group">
  19. <input type="password" id="password" class="form-control" placeholder="Enter Password"/>
  20. </div>
  21. <div id="res">
  22. <span id="chk_capital" class="alert-danger"></span>
  23. <br />
  24. <span id="chk_number" class="alert-danger"></span>
  25. <br />
  26. <span id="chk_length" class="alert-danger"></span>
  27. </div>
  28. <div class="form-group">
  29. <button type="button" onclick="validatePassword()" id="btn_check" class="btn btn-primary form-control">Validate</button>
  30. </div>
  31. </form>
  32. </div>
  33. </div>
  34. </body>
  35. <script src="script.js"></script>
  36. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will validate the password you have entered in the textbox. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. document.getElementById("res").style.display="none";
  2. function validatePassword(){
  3. let chk_capital = document.getElementById('chk_capital');
  4. let chk_number = document.getElementById('chk_number');
  5. let chk_length = document.getElementById('chk_length');
  6. let bool_capital;
  7. let bool_number;
  8. let bool_length;
  9. let password = document.getElementById('password');
  10.  
  11. if(password.value.length > 12){
  12. chk_length.removeAttribute('class');
  13. chk_length.setAttribute('class', 'alert-success');
  14. chk_length.innerHTML = "&#10004; Must be a 12 characters long";
  15. bool_length = true;
  16. }else{
  17. chk_length.removeAttribute('class');
  18. chk_length.setAttribute('class', 'alert-danger');
  19. chk_length.innerHTML = "X Must be a 12 characters long";
  20. bool_length = false;
  21. }
  22.  
  23. if(password.value.search(/[0-9]/) > 0){
  24. chk_number.removeAttribute('class');
  25. chk_number.setAttribute('class', 'alert-success');
  26. chk_number.innerHTML = "&#10004; Must have a number digit";
  27. bool_number = true;
  28. }else{
  29. chk_number.removeAttribute('class');
  30. chk_number.setAttribute('class', 'alert-danger');
  31. chk_number.innerHTML = "X Must have a number digit";
  32. bool_number = false;
  33. }
  34.  
  35. if(password.value.match(/[A-Z]/)){
  36. chk_capital.removeAttribute('class');
  37. chk_capital.setAttribute('class', 'alert-success');
  38. chk_capital.innerHTML = "&#10004; Must have one Uppercase letter";
  39. bool_capital = true;
  40. }else{
  41. chk_capital.removeAttribute('class');
  42. chk_capital.setAttribute('class', 'alert-danger');
  43. chk_capital.innerHTML = "X Must have one Uppercase letter";
  44. bool_capital = false;
  45. }
  46.  
  47. document.getElementById("res").style.display="inline-block";
  48.  
  49. if(bool_capital && bool_length && bool_number){
  50. document.getElementById('btn_check').innerHTML = "Password Validated";
  51. }else{
  52. document.getElementById('btn_check').innerHTML = "Validate";
  53. }
  54.  
  55. }

In the code above we just simply created one method that enable the validation of password you have entered. We will name the method as validatePassword(), this function will only be triggered when the button is clicked. Inside the function we just simply use multiple conditional statement to track the entered character in the textbox, to check if it is allowed to be validated.

Output:

The How to Create a Password Validator in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create a Password Validator in JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment