How would I repeat a block of code in Javascript? (asking users for a username and password)

StackOverflow https://stackoverflow.com/questions/22551966

  •  18-06-2023
  •  | 
  •  

Pergunta

I want to have a repeat function around this bit of Javascript code:

<SCRIPT language="JavaScript">

    var usernamecheck="username";
    var passcheck="password"; 
    var username=prompt ('Enter Username',' ');
    var password=prompt ('Enter Password',' ');
    if 
        (username+password==usernamecheck+passcheck) ;
    else
        {window.location="http://www.google.com";}

</SCRIPT>

I want this bit of code to repeat 3 times if the user inputs an incorrect username or password in.

I would also want to inform the user that the password or username they have entered is incorrect and tell them to try again. When they have already had 3 tries they will then be sent to another webpage e.g."http://www.google.com" .

How would I do this? (Please help)

Foi útil?

Solução 2

I have solved it myself! using the answer of Biochemist_HK and making it work:

        var usernamecheck="name";
        var passcheck="password"; 
        var username=prompt ('Enter Username',' ');
        var password=prompt ('Enter Password',' ');

        //create a counter var
        var counter = 2;

        //Next create a for loop to loop 3 times then if still the credidentials
        //are incorrect then send user to google.com
        for (var i = 0; i < counter; i++){
            if 
                (username+password==usernamecheck+passcheck);

            else {
                alert("The username or password you have entered is incorrect. Please try again.");
                var username=prompt ('Enter Username',' ');
                var password=prompt ('Enter Password',' ');
            }   
        }

        if
            (username+password==usernamecheck+passcheck);

        else //tells user that the username or password they have entered is wrong and will be sent to google.co.nz
            {alert("The username or password you have entered is incorrect. You will now be directed to another webpage.");
            {window.location="http://www.google.co.nz"};};

Thanks for the ones who replied. :)

Outras dicas

Can't you just use a for loop?

For example:

<SCRIPT language="JavaScript">

var usernamecheck="username";
var passcheck="password"; 
var username=prompt ('Enter Username',' ');
var password=prompt ('Enter Password',' ');

//create a counter var
var counter = 3;

//Next create a for loop to loop 3 times then if still the credidentials
//are incorrect then send user to google.com
for (var i = 0; i < counter; i++){
    if {
      (username+password==usernamecheck+passcheck) ;
      //put in code to terminate loop
    }
    else
      var username=prompt ('Enter Username',' ');
      var password=prompt ('Enter Password',' ');}
}

window.location="http://www.google.com";
</SCRIPT>

I agree with everyone else that this is not a good means of site protections, also I am not familiar with javascript language (I program in Java) but it seems familiar enough.

Also check the logic like someone else stated above

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top