-2

Hello I've been trying to write a function to validate a string value. My code is below

function verifyPassword(){
passW = prompt ("Password:");

if (passW = 'Pass123'){
  document.write ('Your password is correct');
}
else {

  document.write ('Your password is incorrect');
}
}

verifyPassword();

But here I always seem to get the result as 'Your password is correct', no matter what I put.

Please can someone help me to resolve this issue?

2 Answers2

2

The comparation-operator in Javascript is ==. The = operator is for assignment and returns the assigned value. So your code is equivalent to:

passW = 'Pass123';
if (passW){
  document.write ('Your password is correct');
}

Use if (passW == 'Pass123') instead.

By the way, I hope you are not really trying to implement authentication and access control with client-sided javascript.

Philipp
  • 67,764
  • 9
  • 118
  • 153
-1

do

function verifyPassword(){
passW = prompt ("Password:");

if (passW == 'Pass123'){
  document.write ('Your password is correct');
}
else {

  document.write ('Your password is incorrect');
}
}

verifyPassword();
Jasmin Mistry
  • 1,459
  • 1
  • 18
  • 23