My requirements are this:
- at least 8 characters,
- must contain at least 1 lowercase
- must contain at least 1 uppercase
- must contain at least one numeral
- must contain at least one of the following set:
!@#$%^&*
- must not contain "and" or "end"
That last part is the part that is tripping me up I googled a bunch and came up with:
^(?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z])\S{8,}$
But I believe that it will allow ALL special characters because of \S
if I understand that post. I can't have that. I also need to check to see if the string contains and
or end
.
Currently I have this. and it works, but it's not elegant.
package testing;
import java.util.Scanner;
public class PasswordTest {
public static void main(String args[]){
System.out.println("Start");
Scanner sc = new Scanner(System.in);
boolean done = false;
while(!done){
boolean size = false;
boolean upper = false;
boolean lower = false;
boolean special = false;
boolean andEnd = false;
boolean numeric = false;
String password = sc.nextLine();
if(password.length()>=8){
size=true;
}
for(int x = 0;x < password.length();x++){
if(password.charAt(x)>= 65 && password.charAt(x)<=90){
upper = true;
}
if(password.charAt(x)>= 97 && password.charAt(x)<=122){
lower = true;
}
if(password.charAt(x)>= 48 && password.charAt(x)<=67){
numeric = true;
}
if(password.charAt(x)>= 33 && password.charAt(x)<=42){
special = true;
}
}
if(!password.contains("and") && !password.contains("end")){
andEnd = true;
}
if(!size){
System.out.println("Password too short");
}
if(!upper){
System.out.println("Password does not contain an uppercase character");
}
if(!lower){
System.out.println("Password does not contain an lowercase character");
}
if(!numeric){
System.out.println("Password does not contain a number");
}
if(!special){
System.out.println("Password does not contain a Special character");
}
if(!andEnd){
System.out.println("Password does contain 'and' or 'end'");
}
done = size && upper && lower && numeric && special&& andEnd;
if(done){
System.out.println("Valid "+password);
}else{
System.out.println("Invalid "+password);
}
}
sc.close();
}
}