这是针对正在采取的Java脚本课程...Javascript:从数组获取索引值
我需要创建一个简单的用户登录脚本。当页面加载提示时弹出询问用户名称。如果输入正确的用户名,则会显示另一个提示,要求输入密码。
如果用户名和有效,那么你一个成功的消息,否则你会得到一个失败的消息。
我写了所有的代码,但在验证用户名和密码时遇到了问题。 你可以在我的代码中看到我正在使用两个数组列表。一个用于用户,另一个用于密码。当我运行我的代码时,如果我为user1键入正确的用户名和密码,它会验证,但如果我输入user1和user2的密码,它仍然有效。
<script type="text/javascript">
//Input from user
var userId = prompt("Enter UserID", "");
userId_lowercase = userId.toLowerCase();
//Username and Password Arrays
var userIdList = new Array();
userIdList[0] = "user1";
userIdList[1] = "user2";
userIdList[2] = "user3";
var passwordList = new Array();
passwordList[0] = "pass1";
passwordList[1] = "pass2";
passwordList[2] = "pass3";
//Process input and check for authentication
//Check for correct userId
var userIdFound;
for (index in userIdList)
{
if (userId_lowercase == userIdList[index])
{
userIdFound = "Y";
break;
}
}
if (userIdFound == "Y")
{
document.write("<p>" + userId + " was Found</p>"); ;
//Check for correct Password
var password = prompt("Enter Password", "");
var passwordFound;
for (index in passwordList)
{
if (password == passwordList[index]) // This is where I need help,
// how do I say
// "if password is from the passwordList && it matches the userId index selected"
{
passwordFound = "Y";
break;
}
}
if (passwordFound == "Y")
{
document.write("<p>Welcome to the class!</p>");
}
else
{
document.write("<p>Error: Password is not valid.</p>");
}//END Password Check
}
else
{
document.write("<p>" + userId + " was NOT found</p>");
}//END USERID Check
</script>
登录形式很容易被欺骗。 “功课”让你感觉如此不切实际,这真让人遗憾。 –
@NSfY:顺便说一句,谢谢你让我们知道这是一个家庭作业问题。实际上没有人这样做。 – outis