Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    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.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,173
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,173
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    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.

  • #5
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    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)

  • #6
    Banned
    Join Date
    Oct 2012
    Posts
    81
    Thanks
    0
    Thanked 4 Times in 4 Posts
    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.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,173
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    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.

  • #8
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,173
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    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.

  • Users who have thanked Philip M for this post:

    adityavishnu (11-13-2012)

  • #10
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •