我需要确保它以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
}
}
最佳答案
在最后一次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");
}
关于javascript - 有时会说密码在实际有效时无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5943201/