Limit User Login Attempt Using JavaScript

This article will teach you how to create Limit User Login Attempt using JavaScript. The program will prevent you to login again after many failed attempts. This is a free code that can be modify, and you use this as your coding references. To learn more about this, just follow the steps below.

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="https://sourcecodester.com">Sourcecodester</a>
  9.                 </div>
  10.         </nav>
  11.        
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">Limit User Login Attempt Using JavaScript</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-6">
  17.                         <form>
  18.                                 <div class="form-group">
  19.                                         <label>Username:</label>
  20.                                         <input type="text" class="form-control" id="username" />
  21.                                 </div>
  22.                                 <div class="form-group">       
  23.                                         <label>Password :</label>
  24.                                         <input type="password" class="form-control" id="password"/>
  25.                                 </div>
  26.                                 <span id="msg"></span>
  27.                                 <br />
  28.                                 <center><button class="btn btn-primary" type="button" id="login" onclick="userLogin();">Login</button></center>
  29.                                 <br />
  30.                                 <center><button class="btn btn-success" type="button" id="reset" onclick="resetAll();" style="display:none;">Reset</button></center>
  31.                         </form>
  32.                 </div>
  33.                 <div class='col-md-3'>
  34.                         <h5>Login details<h5>
  35.                         <h6>Username: admin</h6>
  36.                         <h6>Password: admin</h6>
  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. The code will limit your login attempt in the HTML form. 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("msg").innerHTML="";
  9.         document.getElementById("reset").style.display="none";
  10. }
  11.  
  12.  
  13. function userLogin(){
  14.         var username = document.getElementById("username").value;
  15.         var password = document.getElementById("password").value;
  16.  
  17.         if(username=="" || password==""){
  18.                 alert ("Please complete the required field!");
  19.         }else{
  20.                 if (username == "admin" && password == "admin"){
  21.                         alert ("Login successfully");
  22.                 }else{
  23.                         attempt --;
  24.                         document.getElementById("msg").innerHTML="<center class='text-danger'>Invalid username or password</center>";
  25.                         alert("You have left "+attempt+" login attempt;");
  26.                         if(attempt == 0){
  27.                                 document.getElementById("username").disabled=true;
  28.                                 document.getElementById("password").disabled=true;
  29.                                 document.getElementById("login").disabled=true;
  30.                                 document.getElementById("reset").style.display="inline";
  31.                         }
  32.                 }
  33.         }
  34. }
There you have it we successfully created a Limit User Login Attempt 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