【问题标题】:Password should not start with number密码不应以数字开头
【发布时间】:2019-11-16 12:38:18
【问题描述】:

我希望密码不应以数字开头,但不能按预期工作。

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

【问题讨论】:

    标签: javascript passwords


    【解决方案1】:

    每当第一个字母不为 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");
    }

    【讨论】:

      【解决方案2】:

      您可以使用正则表达式来验证密码。希望对您有所帮助。

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

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

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

      【讨论】:

      • should not start with numbersshould start with alphabets 有区别。
      【解决方案3】:

      欢迎来到 StackOverflow!

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

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

      但是,让我们迭代循环一次。

      i => 0

      nums[i] => 0

      input.charAt(0) => 2

      nums[i] != input.charAt(0) --> true, 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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

          你的问题是你打破了你的 for 循环。例如,如果您输入了3abc 之类的密码,您的循环将首先检查是否为'3' != 0。这是真的,因此您的“有效”警报将执行,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");

          【讨论】:

            【解决方案6】:

            您可以使用像这样的正则表达式 ^[^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
            

            【讨论】:

            • /^[^0-9]/ 就够了。
            • 这提示错误答案。当输入的密码不以数字开头时,提示密码不应该以数字开头。将 [^0-9] 更改为 [0-9] 或翻转 cmets。无论哪种方式都很好
            猜你喜欢
            • 1970-01-01
            • 2016-02-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-30
            • 1970-01-01
            • 2013-05-29
            相关资源
            最近更新 更多