2011-05-09 2 views
2

Z으로 시작해야하며 8 자 이상의 최소 문자가 있고 *입니다.실제로 암호가 유효하지 않은 경우 암호가 올바르지 않다고 말합니다.

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 

} 
} 
+0

완료? 어떤 테스트 암호로 오류가 발생합니까? – MJB

+0

예상대로 유효성이 확인되지 않은 예제 비밀번호를 제공하십시오. – Oswald

+2

또한 루프를 영원히 반복하거나 유효한 암호를 입력하기 전까지는 원하는 것처럼 보입니다. 현실 세계에 대한 추악한 종류 - 나는 그것이 진짜가 아니기를 바랍니다. – MJB

답변

1

당신은 strPassword.length < 7strPassword.length < 8을해야하거나 다른 요구 사항에 실패 않습니다 이 기능을 고려?

EDIT : 유효한 암호에 대한 테스트를 분리하고 각각에 대해보다 의미있는 메시지를 인쇄합니다. 그러면 왜 실패하는지보아야합니다.

+0

방금 ​​수정했습니다. – Cool66

2

마지막 OR 확인시 {이 두 개인 경우 괄호가 너무 많습니다.

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

이 한 때 실패하는

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(); 
관련 문제