I am making a Password/Key Validator with a set of rules which must be met to validate the Password/Key.
These rules are the following: - The key be at least 7 characters long, AND at most 20 characters long, AND - The key must not start with the special characters '#' or '', AND - The key must not have a space character anywhere, AND - The key must have at least one Upper case character and at least one Lower case character, AND - The key must not contain the user's name, AND - The key must contain either a '#' or a '', but not both.
I have managed to get all the rules to work. There are two outputs that checks the key against the rules and either reports that the key is valid or reports ALL the rules that the key failed.
On the output, I am unable to show/report ALL the rules that the key failed.
The code I currently have is below. I am new to learning java so please understand.
* 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.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("^[^#_]+[#_]{1,1}[^#_]*")) {
UI.println("Invalid: Key must contain either a '#' or a '_', but not both");
}
else {
UI.println("Valid");
}
}
Aucun commentaire:
Enregistrer un commentaire