I have a simple form where one predefined password will show hidden div. I would like to have several valid passwords instead of just one. I have researched and maybe an array of passwords can be checked with includes() function?
this is my code for ONE password:
<div id="passw">
<div>
<input type="password" id="password" placeholder="Password Enter" onkeydown="if (event.keyCode == 13) document.getElementById('button').click()" /> <!-- IMPORTANT! this part is so if you click enter, it works. -->
</div>
<div>
<input id="button" type="hidden" value="Submit" onclick="if (document.getElementById('password').value == 'password123') {
document.getElementById('section-hidden').classList.toggle('show');
document.getElementById('passw').style.display='none';
} else {
alert('Invalid Password!'); password.setSelectionRange(0, password.value.length);
} " />
</div>
CodePudding user response:
Yes, you can use the includes()
method and check if the entered password is included in the array.
Simple use of includes():
const passwords = ["password123", "password456"];
if (passwords.includes("password123")) {
console.log("Good password");
}
if (passwords.includes("password999")) {
console.log("Good password");
}
else {
console.log("Bad password!");
}
So just add some passwords
array, and change the condition of the if
statement to this:
if(passwords.includes(document.getElementById('password').value))