不知道如何在不需要时修复弹出消息

时间:2012-12-17 16:59:19

标签: javascript html login

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

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;
}

2 个答案:

答案 0 :(得分:0)

else块仅适用于第二次检查

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;
}

答案 1 :(得分: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脚本或数据库中。