制作密码验证器和规则之一是:密钥必须包含"#"或"_",但不能同时包含两者



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

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

除了最后一条规则"密钥必须包含'#'或'_',但不能同时包含两者。">

我目前拥有的代码如下。我是学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");
}

}

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

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

试试下面的方法,并在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");
}

}

相关内容

  • 没有找到相关文章

最新更新