开发者

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

}
}


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");
}


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.


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜