這是針對正在採取的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