如果错误,请再次请求用户名和密码

时间:2014-04-06 13:39:51

标签: javascript

如果密码和/或用户名错误,我该怎么做才会再次要求输入密码和/或用户名?

var user = prompt("Please Enter Your Username To Continue.").toUpperCase();

if (user === "RYEBREAD4") {
    var pass = prompt("Please Enter Your Password To Continue").toUpperCase();
    if (pass === "7277") {
        console.log("Welcome Back Ryebread4");
    } else {
        console.log("Username Or Password Is Incorrect! Please Try Again");
    }
}

1 个答案:

答案 0 :(得分:1)

使用while循环

var signedIn = false;
while(!signedIn)
{
...
    if (password === "7477")
    {
        console.log("Welcome back!");
        signedIn = true;
     }
...
}

for(不推荐)

 var username = "";
 var password = "";

for(;username !== "Ryebread4" && password !== "7477";)
{
     //prompt user for username/password
      ...
}

同样的方法可以应用于while循环,并消除变量。