第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何檢查給定的用戶名和密碼是否是用戶輸入的內(nèi)容并提示成功或失敗?HTML 和 JS

我如何檢查給定的用戶名和密碼是否是用戶輸入的內(nèi)容并提示成功或失???HTML 和 JS

手掌心 2022-11-11 14:46:04
如標(biāo)題所述,我如何檢查給定的用戶名和密碼是否是用戶輸入的內(nèi)容并提示成功或失???警報不再彈出。我將 js 腳本嵌入到 html 文件中。這是腳本:        function login() {            var username = ["user1", "user2", "user3"];            var password = ["1", "2", "3"];            var idInput = document.getElementById("id");            var pwInput = document.getElementById("pw");            if (username[0] == idInput) {                if (password[0] == pwInput) {                    alert("login success!");                } else if (username[1] == idInput) {                    if (password[1] == pwInput) {                        alert("login success!");                        } else if (username[2] == idInput) {                        if (password[2] == pwInput) {                            alert("login success!");                                                    } else {                             alert("please try again");                         }                    } else {                         alert ("please try again");                    }                 } else {                     alert ("please try again");                }               }        }這是通過 html 和按鈕輸入的正文:<table>    <tr>        <td>Login Id:</td>        <th>                <INPUT type="text" Size="40" Maxlength="35" id="id" placeholder="Your Username Here">        </th>    </tr>    <tr>        <td>Password:</td>        <th>        <INPUT type="text" Size="40" Maxlength="40" id="pw" placeholder="Your Password Here">        </th>    </tr></table><button onclick="login()">Login</button>
查看完整描述

3 回答

?
喵喔喔

TA貢獻1735條經(jīng)驗 獲得超5個贊

您需要更改以下幾行。您正在使用對象,但不是輸入的值。您需要引用對象的 value 屬性才能獲得實際輸入。

var idInput = document.getElementById("id").value;
var pwInput = document.getElementById("pw").value;


查看完整回答
反對 回復(fù) 2022-11-11
?
躍然一笑

TA貢獻1826條經(jīng)驗 獲得超6個贊

寫這個 if 的更好方法是


if(idInput == username[0] && pwInput == password[0]){


}

else if(idInput == username[1] && pwInput == password[1]{


}


else if(idInput == username[2] && pwInput == password[2]{


}

else {

    alert(“User or password wrong.”);

}


如果您有可變數(shù)量的用戶,則可以執(zhí)行循環(huán):


function loginCheck(username, password, idInput, pwInput){

for(i = 0; i <  username.length; i++){

    if(username[i] == idInput && password[i] == pwInput)

        return 1;

}


return 0;

}


現(xiàn)在是一個安全問題:永遠不要在客戶端進行此檢查。不良用戶可以嘗試閱讀您的代碼并獲得訪問權(quán)限。更好的方法是在將加密數(shù)據(jù)發(fā)送到 Web 服務(wù)器檢查(例如 PHP)之前,使用哈希(推薦 SHA 256)對 idInput 和 pwInput 進行加密。因此,您可以防止不良用戶訪問,并且您的用戶可以防止密碼泄露。


查看完整回答
反對 回復(fù) 2022-11-11
?
拉丁的傳說

TA貢獻1789條經(jīng)驗 獲得超8個贊

首先,您試圖評估元素而不是它們的價值。此外,您還有很多不必要的條件。我們還想重組我們存儲登錄憑據(jù)的方式,使其更直觀。


function login() {

    // Instead of storing usernames and passwords in separate arrays,

    // we instead will store them as an array of objects with username

    // and password properties

    var logins = [

      { username: 'user1', password: '1' },

      { username: 'user2', password: '2' },

      { username: 'user3', password: '3' }

    ];

    

    // Get the values of our inputs

    var idInput = document.getElementById("id").value;

    var pwInput = document.getElementById("pw").value;

    

    // The find method on array returns the first occurrence in the

    // array if the condition evaluates to true. In this case, we

    // need to make sure the username and password match what was 

    // entered. The !== undefined determines whether or not find

    // returned a match that met the search criteria.

    const success = logins.find(login => login.username == idInput && login.password == pwInput) !== undefined;

    

    // If it found a match, then we alert the appropriate response.

    if(success) {

      alert('Successful login!');

    } else {

      alert('Failed login!');

    }

}

<table>

    <tr>

        <td>Login Id:</td>

        <th>        

        <INPUT type="text" Size="40" Maxlength="35" id="id" placeholder="Your Username Here">

        </th>

    </tr>

    <tr>

        <td>Password:</td>

        <th>

        <INPUT type="text" Size="40" Maxlength="40" id="pw" placeholder="Your Password Here">

        </th>

    </tr>

</table>


<button onclick="login()">Login</button>


查看完整回答
反對 回復(fù) 2022-11-11
  • 3 回答
  • 0 關(guān)注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號