Cool66
Cool66

Reputation: 21

It sometimes say the password is invalid when it is actually valid

I need to make sure it begins with a Z, has 8 minimum characters and has an *. Consider this function:

function validatePassword()    
{
var strPassword 

//Request user enter their password then check its validity

strPassword = prompt("Please Enter A Valid Password","");

while ((strPassword.length <7) || (strPassword.indexOf('*') ==-1)  || (strPassword.charAt(0) != 'Z')) {
{
alert("Your password is invalid, \n Please try again")
strPassword = prompt("Please Enter A Valid Password","");
}

//Outcome if password is valid

alert("Your password is valid")

//End while

}
}

Upvotes: 2

Views: 184

Answers (3)

mplungjan
mplungjan

Reputation: 178421

This one is complete

http://jsfiddle.net/mplungjan/mvwRj/

function validatePassword() {
  var strPassword;

  //Request user enter their password then check its validity

  strPassword = prompt("Please Enter A Valid Password - Starts with Z minimum 8 chars including an *","");

  while (strPassword==null || strPassword.length <8 ||
                               strPassword.indexOf('*') ==-1  ||
                               strPassword.charAt(0) != 'Z') {
    alert("Your password is invalid, \n Please try again")
    strPassword = prompt("Please Enter A Valid Password","");
  }   //End while


  //Outcome if password is valid

  alert("Your password is valid")

}
validatePassword();

Upvotes: 0

p.campbell
p.campbell

Reputation: 100657

You've got a double { at the last OR check. Too many parenthesis.

function validatePassword()    
{
    var strPassword  = prompt("Please Enter A Valid Password","");
    while ((strPassword.length <7) || 
        (strPassword.indexOf('*') ==-1)  || 
        (strPassword.charAt(0) != 'Z'))
    {
        alert("Your password is invalid, \n Please try again");
        strPassword = prompt("Please Enter A Valid Password","");
    }
    alert("Your password is valid");
}

Upvotes: 2

Tony
Tony

Reputation: 10357

You have strPassword.length < 7 which should be strPassword.length < 8 or does it fail on other requirements?

EDIT: I would separate out the tests for the valid password and print out a more meaningful message for each one. Then you should see why it fails.

Upvotes: 1

Related Questions