JavaScript - Restricting Password Length

Submitted by: 
Visitors have accessed this post 139 times.

In this tutorial we will create a Restricting Password Length using JavaScript. This code will automatically validate the password input when user enter some value in the textbox. The code itself use onkeyup() to initiate a function that will validate the password if whether the character length is acceptable to registered This code is free to use, you can modify it as your own,

We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer .

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design

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="<a href="">Sourcecodester</a>
  10. " rel="nofollow">">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Restricting Password Length</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <form>
  20. <div class="form-group">
  21. <label>Username</label>
  22. <input type="text" id="username" class="form-control"/>
  23. </div>
  24. <div class="form-group">
  25. <label>Password</label>
  26. <input type="password" class="form-control" id="password" onkeyup="passwordValidate(this)"/>
  27. </div>
  28. <span id="chk_length" class="alert-danger">X Password must be a 12 characters long</span>
  29. <br /><br />
  31. <center><button type="button" id="register" onclick="registerUser();" class="btn btn-primary" disabled="disabled">Register</button></center>
  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 automatically validate form inputs when the inputs is entered. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. function passwordValidate(input){
  2. var chk_length = document.getElementById('chk_length');
  3. var bool_length;
  4. var password = input.value;
  6. if(password.length > 12){
  7. chk_length.removeAttribute('class');
  8. chk_length.setAttribute('class', 'alert-success');
  9. chk_length.innerHTML = "&#10004; Password must be a 12 characters long";
  10. bool_length = true;
  11. }else{
  12. chk_length.removeAttribute('class');
  13. chk_length.setAttribute('class', 'alert-danger');
  14. chk_length.innerHTML = "X Password must be a 12 characters long";
  15. bool_length = false;
  16. }
  19. if(bool_length){
  20. document.getElementById('register').removeAttribute('disabled');
  21. }else{
  22. document.getElementById('register').setAttribute('disabled', 'disabled');
  23. }
  25. }
  27. function registerUser(){
  28. var username=document.getElementById('username').value;
  29. var password=document.getElementById('password').value;
  31. if(username==""){
  32. alert("Username must not be empty");
  33. }else{
  34. alert("You have registered!");
  35. window.location='index.html';
  36. }
  38. }

There you have it we successfully created a Restricting Password Length 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.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.