I must create a java program to check if the password is valid or not based on these conditions:
- to be at least 5 chars long or at most 12 chars long
- to start with an uppercase letter
- to end with two different digits
- to include at least one special character from the following: ! " # $ % & ' ( ) * + -
- to include at least one lowercase letter
This is what I have written so far, and I want to know what's the regular expression to check the second condition (that the password must end with 2 different digits) ?
import java.util.Scanner;
public class PasswordValidation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a password");
String password = sc.next();
if (isValid(password)) {
System.out.println("OK");
} else {
System.out.println("NO");
}
}
public static boolean isValid (String password) {
return password.matches ("^[A-Z](?=.*[a-z])(?=.*[!#$%&'()+-]).{5,12}$");
}
}