...

View Full Version : Resolved Login Form in Javascript



adityavishnu
11-13-2012, 12:40 PM
Hey guys i am trying to create a login form with html and javascript and everything is working except that i am not able to implement maximum attempts to this. It tried nested ifs and for too but didn't succeed:mad:.In the below code i am trying to implement maximum 3 attempts of wrong username or password.

<html>
<head>
<script type="text/javascript">
function login() {
var u=document.getElementById("user").value;
var v=document.getElementById("pass").value;
var i=0;
if(i<3)
{
if (u=="admin" && v=="admin_12")
{alert("Welcome");}
else
{alert("Oops,Try Again");
i++;}}
else{alert("Maximum Attempts Over");}
}
</script>
</head>
<body>
<form>
<b>User Name </b><input type="text" id="user">
<b>Password </b><input type="password" maxlength="8" id="pass">
<p>
<input type="button" value="Login" onclick="login()">
</form>
</body>
</html>

minder
11-13-2012, 12:44 PM
I think this is probably a homework exercise because in the real world there is no way you should be using javascript to count login attempts because it can be very easily bypassed.

If you want to count login attempts properly, then you should be doing this server side with a server side language like PHP etc.

Philip M
11-13-2012, 01:00 PM
There is no point in imposing a limit to the number of password attempts as the user can simply close the page, reopen it and start again.

But to make your code work you need to use a for loop.

Philip M
11-13-2012, 01:02 PM
There is no point in imposing a limit to the number of password attempts as the user can simply close the page, reopen it and start again.

And be aware that anyone can see the correct password with View Source.

But to make your code work you need to use a for loop.

minder
11-13-2012, 01:11 PM
There is no point in imposing a limit to the number of password attempts as the user can simply close the page, reopen it and start again.



Not if it's done properly server side. I think you'll find most if not all online banks will lock out users after 3 failed login attempts after which you have to physically contact the bank and jump through several hoops to prove you are who you say you are before they will re-activate your online access to your accounts. (The 2 banks I bank with both do)

minder
11-13-2012, 01:21 PM
If you really need to do this with javascript you can use a do-while loop and/or cookies. But as I said, doing this with javascript is no-no in the real world.

Philip M
11-13-2012, 02:14 PM
Not if it's done properly server side. I think you'll find most if not all online banks will lock out users after 3 failed login attempts after which you have to physically contact the bank and jump through several hoops to prove you are who you say you are before they will re-activate your online access to your accounts. (The 2 banks I bank with both do)

Yes indeed. But we are talking about a beginner's Javascript. There is no point - it is futile - in limiting the number of attempts in that situation.

adityavishnu
11-13-2012, 04:07 PM
There is no point in imposing a limit to the number of password attempts as the user can simply close the page, reopen it and start again.

And be aware that anyone can see the correct password with View Source.

But to make your code work you need to use a for loop.

Its just homework for me so just for knowledge purpose.I tried with for but i didn't work.

Philip M
11-13-2012, 04:45 PM
<html>
<head>
<script type="text/javascript">

var count = 0;
function login() {
count++;
var u=document.getElementById("user").value;
var v=document.getElementById("pass").value;
if (count > 3) {
alert ("Maximum Attempts Over");
return false;
}
if (u=="admin" && v=="admin_12") {
alert("Welcome");
}
else {
alert("Oops,Try Again");
document.getElementById("user").value = "";
document.getElementById("pass").value = "";
document.getElementById("user").focus();
}
}
</script>

</head>
<body>
<form>
<b>User Name </b><input type="text" id="user">
<b>Password </b><input type="password" maxlength="8" id="pass">
<p>
<input type="button" value="Login" onclick="login()">
</form>
</body>
</html>

adityavishnu
11-13-2012, 04:58 PM
dude it worked.....:thumbsup:
how do get these things done? i mean how do u get ideas?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum