JavaScript - Login Form With Limited Attempts

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

In this tutorial we will create a Login Form With Limited Attempts using JavaScript. This code will track the login attempts of user that failed to login the account. The code use onclick() to call a function that validate the user login whether it is incorrect or correct, if login invalid the counter will start decremented after each incorrect login. Feel free to modify and apply it in your system, this is a user-friendly kind of program

We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

Getting started:

First you have to download bootstrap framework, 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. <head>
  3. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  5. </head>
  6. <nav class="navbar navbar-default">
  7. <div class="container-fluid">
  8. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  9. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  10. </a> </div>
  11. </nav>
  12.  
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Login Form With Limited Attempts</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3">
  18. <h6>Username: admin</h6>
  19. <h6>Password: admin</h6>
  20. </div>
  21. <div class="col-md-6">
  22. <h4>User Login</h4>
  23. <hr style="border-top:1px solid #000;"/>
  24. <form>
  25. <div class="form-group">
  26. <label>Username:</label>
  27. <input type="text" class="form-control" id="username" />
  28. </div>
  29. <div class="form-group">
  30. <label>Password :</label>
  31. <input type="password" class="form-control" id="password"/>
  32. </div>
  33. <center><button class="btn btn-primary" type="button" id="login" onclick="userLogin();">Login</button></center>
  34. <br />
  35. <center><button class="btn btn-success" type="button" id="reset" onclick="resetAll();" style="display:none;">Reset</button></center>
  36. </form>
  37. </div>
  38. </div>
  39. <script src="js/script.js"></script>
  40. </body>
  41. </html>

Creating the Script

This code contains the script of the application. This code will login the user account when the button is clicked. 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. var attempt = 3;
  2.  
  3. function resetAll(){
  4. attempt = 3;
  5. document.getElementById("username").disabled=false;
  6. document.getElementById("password").disabled=false;
  7. document.getElementById("login").disabled=false;
  8. document.getElementById("reset").style.display="none";
  9. }
  10.  
  11.  
  12. function userLogin(){
  13. var username = document.getElementById("username").value;
  14. var password = document.getElementById("password").value;
  15.  
  16. if(username=="" || password==""){
  17. alert ("Please complete the required field!");
  18. }else{
  19. if (username == "admin" && password == "admin"){
  20. alert ("Login successfully");
  21. }else{
  22. attempt --;
  23. alert("Invalid username or password \r\nYou have left "+attempt+" attempt;");
  24.  
  25. if(attempt == 0){
  26. document.getElementById("username").disabled=true;
  27. document.getElementById("password").disabled=true;
  28. document.getElementById("login").disabled=true;
  29. document.getElementById("reset").style.display="inline";
  30. }
  31. }
  32. }
  33. }

There you have it we successfully created a Login Form With Limited Attempts 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.