Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-13-2012, 11:40 AM   PM User | #1
adityavishnu
New to the CF scene

 
Join Date: Nov 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
adityavishnu is an unknown quantity at this point
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.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>

Last edited by adityavishnu; 11-13-2012 at 04:13 PM..
adityavishnu is offline   Reply With Quote
Old 11-13-2012, 11:44 AM   PM User | #2
minder
Banned

 
Join Date: Oct 2012
Posts: 81
Thanks: 0
Thanked 4 Times in 4 Posts
minder can only hope to improve
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.
minder is offline   Reply With Quote
Old 11-13-2012, 12:00 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 11-13-2012, 12:02 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 11-13-2012, 12:11 PM   PM User | #5
minder
Banned

 
Join Date: Oct 2012
Posts: 81
Thanks: 0
Thanked 4 Times in 4 Posts
minder can only hope to improve
Quote:
Originally Posted by Philip M View Post
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 is offline   Reply With Quote
Old 11-13-2012, 12:21 PM   PM User | #6
minder
Banned

 
Join Date: Oct 2012
Posts: 81
Thanks: 0
Thanked 4 Times in 4 Posts
minder can only hope to improve
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.
minder is offline   Reply With Quote
Old 11-13-2012, 01:14 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by minder View Post
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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 11-13-2012, 03:07 PM   PM User | #8
adityavishnu
New to the CF scene

 
Join Date: Nov 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
adityavishnu is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
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.
adityavishnu is offline   Reply With Quote
Old 11-13-2012, 03:45 PM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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>
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
adityavishnu (11-13-2012)
Old 11-13-2012, 03:58 PM   PM User | #10
adityavishnu
New to the CF scene

 
Join Date: Nov 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
adityavishnu is an unknown quantity at this point
Thumbs up

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

Last edited by adityavishnu; 11-13-2012 at 04:14 PM..
adityavishnu is offline   Reply With Quote
Reply

Bookmarks

Tags
html, javascript, login

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:18 PM.


Advertisement
Log in to turn off these ads.