Skip to main content

JavaScript - Login Form With Limited Attempts

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

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="">Sourcecodester</a>
  9. </div>
  10. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Login Form With Limited Attempts</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-3">
  17. <h6>Username: admin</h6>
  18. <h6>Password: admin</h6>
  19. </div>
  20. <div class="col-md-6">
  21. <h4>User Login</h4>
  22. <hr style="border-top:1px solid #000;"/>
  23. <form>
  24. <div class="form-group">
  25. <label>Username:</label>
  26. <input type="text" class="form-control" id="username" />
  27. </div>
  28. <div class="form-group">
  29. <label>Password :</label>
  30. <input type="password" class="form-control" id="password"/>
  31. </div>
  32. <center><button class="btn btn-primary" type="button" id="login" onclick="userLogin();">Login</button></center>
  33. <br />
  34. <center><button class="btn btn-success" type="button" id="reset" onclick="resetAll();" style="display:none;">Reset</button></center>
  35. </form>
  36. </div>
  37. </div>
  38. <script src="js/script.js"></script>
  39. </body>
  40. </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;
  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. }
  12. function userLogin(){
  13. var username = document.getElementById("username").value;
  14. var password = document.getElementById("password").value;
  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;");
  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!


Submitted by4linAdv (not verified)on Mon, 05/24/2021 - 19:24


Add new comment