Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Checking input value against several values using .includes()

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>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading