密码不能以数字开头

Password should not start with number

我希望密码不以数字开头,但它没有按预期工作。

var input = prompt("Enter password");
var nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

for (var i = 0; i < nums.length; i++) {
  if (input.charAt(0) != nums[i]) {
    console.log("password is valid");
    break;
  } else
    console.log("Password should not start with number");
}

您可以使用正则表达式进行验证password.I希望对您有所帮助。

^[A-Za-z]\w*$

或者你也可以使用这个正则表达式。

^(?![0-9_])\w+$

每次第一个字母不为 0(第一个 nums 数组项)时,都会立即触发您的有效密码警报。有多种方法可以做到这一点,但这里有一种使用 isNaN 和 parseInt 函数进行检查的更简单的方法:

var input = prompt("Enter password");

if (isNaN(parseInt(input[0]))) {
  alert("password is valid");
} else {
  alert("Password should not start with number");
}

欢迎使用 Whosebug!

假设您输入的是“2hello”。

您的代码所做的是检查您输入的第一个字符是否与 nums 数组中的元素不同。

但是,让我们重复一次循环。

我 => 0

nums[i] => 0

input.charAt(0) => 2

nums[i] != input.charAt(0) --> 正确,2 != 0

那就有效了。

但让我们试试这个:

var input = prompt("Enter password");
var nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

if (!nums.includes(parseInt(input.charAt(0)))) {
    alert("password is valid");
    break;
}
else 
    alert("Password should not start with number");

如您所见,您不需要 for 循环。您有一个内置函数 ("includes") 可以检查您分配的数组中是否有该字符。

PS: 不要忘记将字符转换为整数!当您输入密码时,它是一个字符串。在此处查看更多信息:parseInt

你的问题是你在 for 循环中中断了。例如,如果您输入密码 3abc,您的循环将首先检查是否 '3' != 0。这是真的,因此您的 "valid" 警报将执行并且 break 退出 for 循环。

另一种解决方案是使用 .some().startsWith() 来检查您的输入是否以任何数字开头。然后如果是,则输出关联的结果:

const input = prompt("Enter password");
const nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

const isValidPassword = !nums.some(n => input.startsWith(n));

if (isValidPassword)
  alert("password is valid");
else
  alert("Password should not start with number");

或者,您可以使用正则表达式简单地测试您的第一个字符是否与数字匹配:

const [c] = prompt("Enter password");
const nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

const isValidPassword = !/[0-9]/.test(c);

if (isValidPassword)
  alert("password is valid");
else
  alert("Password should not start with number");

您可以像这样使用正则表达式 ^[^0-9].*$ 在您的代码中使用类似这样的内容。

if (/^[^0-9].*$/.test(input)) {
  alert("password is valid");
} else {
  alert("Password should not start with number");
}

Regex explanation

^ => Match from start position of input
[^0-9] => Other then any number & as it's placed at beginning so anything but number
.* => Match any character
$ => This is end of the entire regular expression & tells it should exact match like this

以下代码修复了您的算法:

function isValidPassword(password, blacklistedChars) {
      const passwordFirstChar = password.charAt(0);

      for(const character of blacklistedChars) {
        if (character === passwordFirstChar) {
          return false;
        }
      }

      return true;
}

const blacklist = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
const validPassword = "secret";
const invalidPassword = "2secret";

console.log("valid password: " + isValidPassword(validPassword, blacklist));
console.log("invalid password: " + isValidPassword(invalidPassword, blacklist));

你也可以考虑使用正则表达式,像这样:

function isValidPassword(password) {
  const invalidPasswordRegex = /^\d/;
  return !invalidPasswordRegex.test(password);
}

const validPassword = "secret";
const invalidPassword = "2secret";

console.log("valid password: " + isValidPassword(validPassword));
console.log("invalid password: " + isValidPassword(invalidPassword));