skip to Main Content

I made a password verification field in js, but it doesn’t work correctly and you can just skip it, and it will say that you entered correct password:

function checkPasOne(password) {

let enteredpas = prompt('Enter password:', '')

if (enteredpas == 673577) { alert('Correct')

    

} else {

    alert('Wrong password')

    return checkPasOne()

}

}

checkPasOne()

I tried to make it using html but it doesn’t work and I need to do it in js. I expected it will don’t let you pass until you enter right password, but it let you pass only if you press cancel and it says you wrong password even if you write correct password.

And can I just remove cancel button and make correct password work

2

Answers


  1. You cannot remove the cancel from prompt() in JS. I ran the code and it seemed to run just fine. What do you mean just skip it? When I pressed cancel, it said wrong password and went back into the loop.

    Login or Signup to reply.
  2. Point #1:
    it will let me pass if I give correct password otherwise it will not let me pass. Therefore I can use simple condition.

    if (enteredpas == 673577) {
          alert("Correct");
          return;
        } else {
          alert("Wrong password");
          checkPasOne();
        }
      }
    

    Point #2: If I press cancel, it will let me pass by showing me wrong password. To do this, we need extra layer of condition. It will check whether ‘enteredpas’ contains any value or not. Because by pressing cencel button it will return null value.

    So the final code will be:

    function checkPasOne(password) {
      let enteredpas = prompt("Enter password:", "");
    
      console.log(enteredpas);
    
      if (enteredpas) {
        if (enteredpas == 673577) {
          alert("Correct");
          return;
        } else {
          alert("Wrong password");
          checkPasOne();
        }
      } else {
        alert("Wrong password");
        return;
      }
    }
    checkPasOne();

    Note: as far as I know, you can’t remove cencel button in prompt.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search