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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Attempts checking problem, JS

    Code:
    
    <html>
    <head>
     
    <script Language="JavaScript">
     
    function passCheck();
    {
    	var myPass = document.getElementById("myPass");
    	i=0;
    	while (myPass!=="Ofir")
    	{
    	i=i+1;
    	}
    	if (i>2)
    	{
    		alert("You have reached the attempts limit, Please try again later");
    	}
    	else if (i<2)
    	{
    		alert("We do not recognize you, Please try again.");
    	}
    }
     
    </script>
     
    </head>

    I've been trying to write a code that recieves a data (password), tho if the person has entered a wrong password
    for over 2 times, he'll get an alert saying he's been banned for a few minutes..

    I dont understand whats wrong with my script, please check this out and help me find a solution?
    Thanks.

    Im a begginer tho, just fyi.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    You have a fallen victim to the wrong positioning of a semi-colon:-

    function passCheck() ;

    This brings your script to an abrupt and unwanted end.

    That is why it is recommended that in Javascript you place the opening brace following the function, if, else, for, while, do, switch, and try statements on the same line and not on the following line. Apart from that every Javascript statement should be followed by a semi-colon (;). It is quite possible to disregard this advice, but if you do one day (like today) it will rise up and bite you in the undercarriage.

    There are several other errors and infelicities as well, and the logic is not correct. <script language = "Javascript"> is long obsolete. These days scripts should be positioned right ahead of the </body> tag. I have re-written your script - please study and learn from it - but like all Javascript password scripts it virtually useless because it has the defect that the user can simply use View Source to see the password. It is possible to obfuscate the password, but that only offers a marginal improvement in protection.

    And of course the user can avoid the 60-second block simply by reloading the page.

    If this is just for learning purposes, that is OK, but Javascript password scripts are almost useless in any real situation. It will work successfully only with the most inexperienced of users.

    alerts are obsolete these days, and should be used only for testing purposes. You should use DOM methods (i.e. write to a <span> using document.getElementById("myspanid").innerHTML = message) to display a message to the user.


    Code:
    <html>
    <head>
    </head>
    <body>
    
    <input type = "password" id = "pwd" onblur = "passCheck()">
     
    <script type = "text/javascript">
    
    var count = 0;
    function passCheck() {
    var myPass = document.getElementById("pwd").value;  // do not use the same name for an HTMl element and a Javascript variable.
    if (myPass != "Ofir") {   // the user can see the password by using View Source
    count ++;
    if (count > 2) {
    alert ("Third wrong password! \nYou have exceeded the maximum number of tries allowed!");
    document.getElementById("pwd").value = "You are now banned!";
    document.getElementById("pwd").disabled = true;
    window.setTimeout (resume,60000);  // 60 seconds = 1 minute
    return false;
    }
    else {
    alert ("Wrong password - please try again!");
    document.getElementById("pwd").value = "";  // clear the field
    document.getElementById("pwd").focus();  // and refocus on it
    return false;
    }
    }
    
    alert ("The password is correct");
    
    }
     
    function resume () {
    document.getElementById("pwd").disabled = false;
    document.getElementById("pwd").value = "";
    }
    
    </script>
     
    </body>
    </html>
    Haikus are easy
    But sometimes they don't make sense.
    Refrigerator.
    Last edited by Philip M; 03-31-2013 at 11:25 AM. Reason: Typo

    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.

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Alright then.
    First of, thanks a lot - you were much help to me!

    Now, I understand what you did in that script however I was not familiar with some of the parameters you used such as Window.setTimeOut, Disabled textbox and some others, which now I see how useful they are.

    One thing I dont understand is the "return" command.
    I have been looking for info around the net about it but just dont get it.
    Mybe you could explain it to me through this script?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by OfirKing View Post
    Alright then.
    First of, thanks a lot - you were much help to me!

    Now, I understand what you did in that script however I was not familiar with some of the parameters you used such as Window.setTimeOut, Disabled textbox and some others, which now I see how useful they are.

    One thing I dont understand is the "return" command.
    I have been looking for info around the net about it but just dont get it.
    Mybe you could explain it to me through this script?
    return means what it says - return a single value back to the location which called it. When a return statement is encountered, the function will stop executing, and return the specified value. In this case return false means return the Boolean value false and stop execution of the function (not necessarily the entire Javascript) right there. Any code that follows the return statement will not be run.

    Example:-

    Code:
    <script type="text/javascript">
    function addem(a,b) {
    return a + b;
    }
    alert (addem(9,6)); //15  
    </script>
    I think that there is a good deal more you will need to know before you will be able to create useful Javascripts.

    window must be spelled as such, not Window. Javascript is case sensitive.
    Last edited by Philip M; 03-31-2013 at 03:47 PM.

    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.


  •  

    Posting Permissions

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