Javascript - Simple Password Validation

Submitted by: 
Language: 
Visitors have accessed this post 308 times.

In this tutorial we will create a Simple Password Validation using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

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

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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">Javascript - Simple Password Validation</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-3"></div>
  17. <div class="col-md-6">
  18. <form>
  19. <div class="form-group">
  20. <label>Enter a password</label>
  21. <input type="password" id="password" class="form-control"/>
  22. </div>
  23. <span id="chk_capital" class="alert-danger">X Must have one capital letter</span>
  24. <br />
  25. <span id="chk_number" class="alert-danger">X Must have a number digit</span>
  26. <br />
  27. <span id="chk_length" class="alert-danger">X Must be a 12 characters long</span>
  28. <br /><br />
  29. <div class="form-group">
  30. <button type="button" onclick="CheckPassword()" id="btn_check" class="btn btn-primary form-control">Check</button>
  31. </div>
  32. </form>
  33. </div>
  34. </div>
  35. </body>
  36. <script src="js/script.js"></script>
  37. </html>

Creating the Script

This code contains the script of the application. This code will validate the password inputs to ensure security of information. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.

  1. function CheckPassword(){
  2. var chk_capital = document.getElementById('chk_capital');
  3. var chk_number = document.getElementById('chk_number');
  4. var chk_length = document.getElementById('chk_length');
  5. var bool_capital;
  6. var bool_number;
  7. var bool_length;
  8. var password = document.getElementById('password');
  9.  
  10. if(password.value.length > 12){
  11. chk_length.removeAttribute('class');
  12. chk_length.setAttribute('class', 'alert-success');
  13. chk_length.innerHTML = "&#10004; Must be a 12 characters long";
  14. bool_length = true;
  15. }else{
  16. chk_length.removeAttribute('class');
  17. chk_length.setAttribute('class', 'alert-danger');
  18. chk_length.innerHTML = "X Must be a 12 characters long";
  19. bool_length = false;
  20. }
  21.  
  22. if(password.value.search(/[0-9]/) > 0){
  23. chk_number.removeAttribute('class');
  24. chk_number.setAttribute('class', 'alert-success');
  25. chk_number.innerHTML = "&#10004; Must have a number digit";
  26. bool_number = true;
  27. }else{
  28. chk_number.removeAttribute('class');
  29. chk_number.setAttribute('class', 'alert-danger');
  30. chk_number.innerHTML = "X Must have a number digit";
  31. bool_number = false;
  32. }
  33.  
  34. if(password.value.match(/[A-Z]/)){
  35. chk_capital.removeAttribute('class');
  36. chk_capital.setAttribute('class', 'alert-success');
  37. chk_capital.innerHTML = "&#10004; Must have one capital letter";
  38. bool_capital = true;
  39. }else{
  40. chk_capital.removeAttribute('class');
  41. chk_capital.setAttribute('class', 'alert-danger');
  42. chk_capital.innerHTML = "X Must have one capital letter";
  43. bool_capital = false;
  44. }
  45.  
  46. if(bool_capital && bool_length && bool_number){
  47. document.getElementById('btn_check').innerHTML = "Password Validated";
  48. }else{
  49. document.getElementById('btn_check').innerHTML = "Check";
  50. }
  51.  
  52. }

There you have it we successfully created a Simple Password Validation using 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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.