All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Login Form With Limited Login Attempts Using JavaScript And HTML

Last Updated : Jul 1, 2023

IN - Javascript HTML | Written & Updated By - Pragati

In this tutorial we will show you how to create login form with limited login attempts using JavaScript and HTML, Websites Like facebook uses login form with limited login attempts in this there is certain number of login attempts if you lost all the attempts then you have to login after some time.

This method is used to increase login form security and prevent spamming on website.

You may also like login form with shake animation effect.

Login Form With Limited Login Attempts Using JavaScript And HTML

To Create Login Form With Login Attempts It Takes Only Two Steps:-

  1. Make a HTML file and define markup and scripting
  2. Make a CSS file and define styling

Step 1. Make a HTML file and define markup and scripting

We make a HTML file and save it with a name login.html

<html>
<head>
<link rel="stylesheet" type="text/css" href="login_style.css">
<script type="text/javascript">
var login_attempts=3;
function check_form()
{
 var name=document.getElementById("name").value;
 var pass=document.getElementById("pass").value;
 if(name=="talkerscode" && pass=="talkerscode")
 {
  alert("SuccessFully Logged In");
  document.getElementById("name").value="";
  document.getElementById("pass").value="";
 }
 else
 {
  if(login_attempts==0)
  {
   alert("No Login Attempts Available");
  }
  else
  {
   login_attempts=login_attempts-1;
   alert("Login Failed Now Only "+login_attempts+" Login Attempts Available");
   if(login_attempts==0)
   {
    document.getElementById("name").disabled=true;
    document.getElementById("pass").disabled=true;
    document.getElementById("form1").disabled=true;
   }
  }
 }
 
 return false;
}	
</script>
</head>
<body>
<div id="wrapper">

<form id="form1" method="post" action="" onsubmit="return check_form();">
 <p id="login_label">USER LOGIN</p>
 <input type="text" id="name" placeholder="Enter Username">
 <br>
 <input type="password" id="pass" placeholder="Enter Password">
 <br>
 <input type="submit" value="SUBMIT FORM">
 <p>Username : talkerscode Password : talkerscode</p>
 <br>
</form>

</div>
</body>
</html>

In this step we create a form to do login with predefined username and password.

We create a 'login_attempts' variable and set the value to 3 it means user have only 3 login attempts we also create check_form function under this we get username and password value and check if it matches our prefined name and password.

If username and password does not match we decrement the attempt by 1 every time and if the attempt value reaches to 0 we disable form and form fields to prevent user for further login.

You may also like flip animated login and signup form.

Step 2. Make a CSS file and define styling

We make a CSS file and save it with a name login_style.css

body
{
 text-align:center;
 width:100%;
 margin:0 auto;
 padding:0px;
 font-family:helvetica;
}
#wrapper
{
 text-align:center;
 margin:0 auto;
 padding:0px;
 width:995px;
}
#form1
{
 background-color:white;
 width:380px;
 margin-left:305px;
 box-shadow:0px 0px 10px 0px #D8D8D8;
}
#form1 p
{
 color:#FA8258;
 font-weight:bold;
}
#form1 #login_label
{
 color:#FA8258;
 padding:20px;
 font-size:25px;
 border-bottom:1px solid #E6E6E6;
 font-weight:bold;
}
#form1 input[type="text"],input[type="password"]
{
 width:300px;
 height:40px;
 padding-left:10px;
 margin-top:10px;
 border:1px solid #D8D8D8;
}
#form1 input[type="submit"]
{
 margin-top:10px;
 width:300px;
 height:40px;
 background-color:#FA8258;
 border:none;
 color:white;
 box-shadow:0px 3px 0px 0px #FE642E;
 border-radius:3px;
 font-weight:bold;
}

Thats all, this is how to create login form with limited login attempts using JavaScript and HTML. You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

I hope this tutorial on login form with limit login attempts using JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪