You need to access the value
of the input field to get the appropriate text value to compare. Also, your logic should check for empty first then wrong.
var pw = "1234";
function checkpw(event, input) {
var x = document.getElementById("pw"); // could be `input` instead
if (x.value === "") {
console.log("no password entered");
} else if (x.value !== pw) {
console.log("wrong password");
} else {
console.log("correct pw");
}
}
<input type="password" id="pw" oninput="checkpw(event, this)">
I recommend wrapping the field in a form form.
Note: Never perform authentication on the client.
const loginForm = document.forms.login;
const expectedPassword = '1234';
const authenticate = (event, form) => {
event.preventDefault();
const actualPassword = new FormData(form).get('password').trim();
console.log(checkPassword(actualPassword));
};
const checkPassword = (actualPassword) => {
if (actualPassword.length === 0) {
return 'No password entered';
} else if (actualPassword !== expectedPassword) {
return 'Wrong password';
} else {
return 'Correct pw';
}
};
<form name="login" onsubmit="authenticate(event, this)" autocomplete="off">
<label for="pw">Password</label>
<input type="password" id="pw" name="password" />
<button type="submit">Submit</button>
</form>