CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Resolved Login Form in Javascript (http://www.codingforums.com/showthread.php?t=282079)

adityavishnu 11-13-2012 11:40 AM

Login Form in Javascript
 
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.
Code:

<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 11:44 AM

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 12: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 12: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 12:11 PM

Quote:

Originally Posted by Philip M (Post 1291538)
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 12: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 01:14 PM

Quote:

Originally Posted by minder (Post 1291541)
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 03:07 PM

Quote:

Originally Posted by Philip M (Post 1291538)
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 03:45 PM

Code:

<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 03:58 PM

dude it worked.....:thumbsup:
how do get these things done? i mean how do u get ideas?


All times are GMT +1. The time now is 12:15 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.