【问题标题】:Making A Password Validator And One Of Rules Is: The Key Must Contain Either A '#' or A '_', But NOT Both制作密码验证器和规则之一是:密钥必须包含“#”或“_”,但不能同时包含两者
【发布时间】:2020-07-02 05:03:19
【问题描述】:

我正在制作一个密码/密钥验证器,其中包含一组必须满足的规则才能验证密码/密钥。

这些规则如下: - 密钥长度至少为 7 个字符,并且最多为 20 个字符,并且 - 键不能以特殊字符 '#' 或 '' 开头,并且 - 密钥在任何地方都不能有空格字符,并且 - 密钥必须至少有一个大写字符和至少一个小写字符,并且 - 密钥不得包含用户名,并且 - 密钥必须包含“#”或“”,但不能同时包含两者。

除了最后一条规则“密钥必须包含 '#' 或 '_',但不能同时包含两者。”之外,我已设法让所有工作都正常工作。

我目前拥有的代码如下。我是学习java的新手,所以请理解。

 * Asks user for key word and the name and then checks if it is a valid key word.
 */
public void doCompletion(){
    String key = UI.askString("Key:   ");
    String name = UI.askString("Your name:   ");
    this.validateKeyCompletion(key, name);
}

/** COMPLETION
 * Report that the key is valid or report ALL the rules that the key failed.
 */
public void validateKeyCompletion(String key, String name){
    /*# YOUR CODE HERE */
   int characterNumber = key.length();
   boolean hasUppercase;
   boolean hasLowercase;
   hasUppercase = !key.equals(key.toLowerCase());
   hasLowercase = !key.equals(key.toUpperCase());
   String specialChars = "(.*[ #  _  ].*)";
   if (characterNumber < 7 || characterNumber > 20){
    UI.println("Invalid: Key length must not be less than 7 or greater than 20");
   }
   else if (key.startsWith ("#") || (key.startsWith ("_"))){
    UI.println("Invalid: Key cannot start with '#' or '_'");
   } 
   else if (key.contains(" ")){
    UI.println("Invalid: Key cannot contain ' '");
   }
   else if(!hasUppercase)
   {
    UI.println("Invalid: Key must contain an uppercase character");
   }
   else if(!hasLowercase)
   {
    UI.println("Invalid: Key must contain a lowercase character");
   }
   else if(key.matches(name)){
    UI.println("Invalid: Key cannot contain Username");
   }
   else if(!key.matches(specialChars)){
    UI.println("Invalid: Key must contain either a '#' or a '_', but not both");
   } 
   else {
    UI.println("Valid");
   }


}

【问题讨论】:

    标签: java string validation if-statement passwords


    【解决方案1】:

    以下正则表达式检查密钥是否以 '#''_' 开头,并且包含其中一个字符,但不能同时包含两者。

        if (!key.matches("^[^#_]+[#_]{1,1}[^#_]*")) {
            UI.println("Invalid: Key must contain either a '#' or a '_', but not both");
        }
    

    【讨论】:

      【解决方案2】:

      在下面试试这个并在 JAVA 中使用 indexOf 方法:

       * Asks user for key word and the name and then checks if it is a valid key word.
       */
      public void doCompletion(){
          String key = UI.askString("Key:   ");
          String name = UI.askString("Your name:   ");
          this.validateKeyCompletion(key, name);
      }
      
      /** COMPLETION
       * Report that the key is valid or report ALL the rules that the key failed.
       */
      public void validateKeyCompletion(String key, String name){
          /*# YOUR CODE HERE */
         int characterNumber = key.length();
         boolean hasUppercase;
         boolean hasLowercase;
         hasUppercase = !key.equals(key.toLowerCase());
         hasLowercase = !key.equals(key.toUpperCase());
         String specialChars = "(.*[ #  _  ].*)";
         if (characterNumber < 7 || characterNumber > 20){
          UI.println("Invalid: Key length must not be less than 7 or greater than 20");
         }
         else if (key.startsWith ("#") || (key.startsWith ("_"))){
          UI.println("Invalid: Key cannot start with '#' or '_'");
         } 
         else if (key.contains(" ")){
          UI.println("Invalid: Key cannot contain ' '");
         }
         else if(!hasUppercase)
         {
          UI.println("Invalid: Key must contain an uppercase character");
         }
         else if(!hasLowercase)
         {
          UI.println("Invalid: Key must contain a lowercase character");
         }
         else if(key.matches(name)){
          UI.println("Invalid: Key cannot contain Username");
         }
         else if(key.indexOf('#') > -1 && key.indexOf('_') > -1){
          UI.println("Invalid: Key must contain either a '#' or a '_', but not both");
         } 
         else {
          UI.println("Valid");
         }
      
      
      }
      

      【讨论】:

      • 您的代码似乎不正确,因为如果key 将同时包含#_,您的支票将返回true
      • 大于 -1 的条件检查字符是否存在,如果两者都存在,则会打印错误消息。
      • 对不起,你是对的。实际上你的检查错过了这种情况,当密钥不包含#,也不包含_,在这种情况下它也返回false
      猜你喜欢
      • 2016-06-10
      • 2017-08-11
      • 2020-05-16
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多