我需要确保它以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 :(得分: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");
}
答案 1 :(得分:1)
您的strPassword.length < 7
应该是strPassword.length < 8
,还是因其他要求而失败?
编辑:我会将有效密码的测试分开,并为每个密码打印出更有意义的消息。然后你应该明白它失败的原因。
答案 2 :(得分: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();