0

我一直在尝试使用 HTML 和 Javascript 进行网站登录,除了一个问题外它工作正常。首次登录信息登录,但弹出else消息显示!我该如何解决?

function Login(form) 
{
    username = new Array("alex364981");
    password = new Array("Codylewis");
    page = "user2" + ".html";

    if (form.username.value == username[0] && form.password.value == password[0]
            || form.username.value == username[1] && form.password.value == password[1]
            || form.username.value == username[2] && form.password.value == password[2]
            || form.username.value == username[3] && form.password.value == password[3]
            || form.username.value == username[4] && form.password.value == password[4]
            || form.username.value == username[5] && form.password.value == password[5]
            || form.username.value == username[6] && form.password.value == password[6]
            || form.username.value == username[7] && form.password.value == password[7]
            || form.username.value == username[8] && form.password.value == password[8]
            || form.username.value == username[9] && form.password.value == password[9])
    {
        self.location.href = page;
    }

    username = new Array("test");
    password = new Array("this");
    page = "user1" + ".html";

    if (form.username.value == username[0] && form.password.value == password[0]
            || form.username.value == username[1] && form.password.value == password[1]
            || form.username.value == username[2] && form.password.value == password[2]
            || form.username.value == username[3] && form.password.value == password[3]
            || form.username.value == username[4] && form.password.value == password[4]
            || form.username.value == username[5] && form.password.value == password[5]
            || form.username.value == username[6] && form.password.value == password[6]
            || form.username.value == username[7] && form.password.value == password[7]
            || form.username.value == username[8] && form.password.value == password[8]
            || form.username.value == username[9] && form.password.value == password[9])
    {
        self.location.href = page;
    }
    else
    {
        alert("Either the Username or Password is Wrong.\nPlease try again.");
        form.username.focus();
    }
    return true;
}
4

2 回答 2

0

else 块只适用于第二个 if 检查

username = new Array("test");
password = new Array("this");
page = "user1" + ".html";

if (form.username.value == username[0] && form.password.value == password[0] || form.username.value == username[1] && form.password.value == password[1] || form.username.value == username[2] && form.password.value == password[2] || form.username.value == username[3] && form.password.value == password[3] || form.username.value == username[4] && form.password.value == password[4] || form.username.value == username[5] && form.password.value == password[5] || form.username.value == username[6] && form.password.value == password[6] || form.username.value == username[7] && form.password.value == password[7] || form.username.value == username[8] && form.password.value == password[8] || form.username.value == username[9] && form.password.value == password[9])

{
    self.location.href = page;
}
else {
    alert("Either the Username or Password is Wrong.\nPlease try again.");
    form.username.focus();
}

您应该从第一个 if 块中成功登录。

您应该使用一个标志来检查是否有任何登录成功,并使用该标志来显示错误消息。

试试这个:

function Login(form) 
{
    flag = false;

    username = new Array("alex364981");
    password = new Array("Codylewis");
    page = "user2" + ".html";

    if (form.username.value == username[0] && form.password.value == password[0] || form.username.value == username[1] && form.password.value == password[1] || form.username.value == username[2] && form.password.value == password[2] ||  form.username.value == username[3] && form.password.value == password[3] || form.username.value == username[4] && form.password.value == password[4] || form.username.value == username[5] && form.password.value == password[5] || form.username.value == username[6] && form.password.value == password[6] || form.username.value == username[7] && form.password.value == password[7] || form.username.value == username[8] && form.password.value == password[8] || form.username.value == username[9] && form.password.value == password[9])

    {
        self.location.href = page;
        flag = true;
    }

    username = new Array("test");
    password = new Array("this");
    page = "user1" + ".html";

    if (form.username.value == username[0] && form.password.value == password[0] || form.username.value == username[1] && form.password.value == password[1] || form.username.value == username[2] && form.password.value == password[2] || form.username.value == username[3] && form.password.value == password[3] || form.username.value == username[4] && form.password.value == password[4] || form.username.value == username[5] && form.password.value == password[5] || form.username.value == username[6] && form.password.value == password[6] || form.username.value == username[7] && form.password.value == password[7] || form.username.value == username[8] && form.password.value == password[8] || form.username.value == username[9] && form.password.value == password[9])

    {
        self.location.href = page;
        flag = true;
    }

    if(!flag) {
        alert("Either the Username or Password is Wrong.\nPlease try again.");
        form.username.focus();
    }
    return true;
}
于 2012-12-17T17:12:19.840 回答
0

试试这个。循环遍历你的数组应该清理代码和可读性。这也将使以后更容易扩展。

function Login(form) 
{
    username = new Array("alex364981");
    password = new Array("Codylewis");
    page = "user2.html";

    var numOfUsers=10;
    for(var i=0;i<numOfUsers;i++)
    {
        if (form.username.value == username[i] && form.password.value == password[i])
        {
            self.location.href = page;
            return true;
        }
    }
    //All possibilities exhausted, Show error.
    alert("Either the Username or Password is wrong.\nPlease try again.");
    return false;
}

希望这可以帮助。

另请注意,用户可以查看您的 html 的源代码并直接从该函数中获取用户名和密码。最好将其隐藏在 php 脚本或数据库中。

于 2012-12-17T17:16:02.807 回答