简体   繁体   中英

Below is the javaScript for login validation. but output always "Incorrect username or password" i.e else condition

 var objLogin = [{ fname: "sastry", book: "sastry" }, { fname: "laxman", book: "laxman" }, { fname: "visali", book: "visali" }] function storeData() { var userName = document.getElementById("fname").Value; var userPasswd = document.getElementById("book").Value; for (i = 0; i < objLogin.length; i++) { if (userName == objLogin[i].fname && userPasswd == objLogin[i].book) { console.log(userName + "Is logged in!!"); return; } else { console.log("Incorrect username or password"); } } }

When accessing the value of an input field, you should use value (lower case "v").

var userName = document.getElementById("fname").value;

Also, it's great to cache your element references, like this:

const userNameInput = document.getElementById("fname");
const userPasswdInput = document.getElementById("book");

function storeData() {
    const userName = userNameInput.value;
    const userPasswd = userPasswdInput.value;

    for (i = 0; i < objLogin.length; i++) {
        if (userName === objLogin[i].fname && userPasswd === objLogin[i].book) {
            console.log(userName + "Is logged in!!");
            return;
        } else {
            console.log("Incorrect username or password");
        }
    }
}

I think there is some typo.

  var userName = document.getElementById("fname").Value;
  var userPasswd = document.getElementById("book").Value;

As far as I know, document.getElementById("something").value is right;

Oh, someone answer that, but it's my first time to use stack overflow, how can I do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM