Wednesday 19 October 2016

JavaScript Form Validation With Limit Login Attempts


index.html

<html> <head>
<title>Javascript Login Form Validation</title> <script src="login.js"></script> </head> <body>
<h2>Javascript Login Form Validation</h2> <form id="form_id" method="post" name="myform"> User Name : <input type="text" name="username" id="username"/> Password : <input type="password" name="password" id="password"/> <input type="button" value="Login" id="submit" onclick="validate()"/> </form> </body>
</html>


login.js

var attempt = 3;


function validate()
{
var username = document.getElementById("username").value; var password = document.getElementById("password").value;
if ( username == "Formget" && password == "formget#123")
{ alert ("Login successfully"); window.location = "success.html"; // Redirecting to other page. return false; }
else
{ attempt --; alert("You have left "+attempt+" attempt;");

if( attempt == 0)
{ document.getElementById("username").disabled = true; document.getElementById("password").disabled = true; document.getElementById("submit").disabled = true; return false; } } }





1 comment: