简体   繁体   中英

Hi how can i check regular expression which accept numbers,characters and special character in java

I have login page that check for password strength. There is an categories i need to check for regular expressions. How can i check in java? The main problem is that when input as "HELLOWORLDHELLO" or 123456789012. It still can be accepted. Not going into false.If input is not equal to at least below two categories. It should be return false. Thanks.Below is my password strength. Password must be more than 12 characters and contains at least 2 of categories below:

        At least 1 upper case character.
        At least 1 lower case character.
        At least 1 digit number.
        At least 1 special character. (!,$,#,%,etc.)

And my java code is below.

public static final String UPPER_CASE_REGEX = "^(?=.*[A-Z])[A-Za-z\\d@$!%*?&^~#*()`<>,.|+-_{}:;\'\"\\/]{12,}$";
public static final String LOWER_CASE_REGEX = "^(?=.*[a-z])[A-Za-z\\d@$!%*?&^~#*()`<>,.|+-_{}:;\'\"\\/]{12,}$";
    
public static final String NUMBER_REGEX = "^(?=.*\\d)[A-Za-z\\d@$!%*?&^~#*()`<>,.|+-_{}:;\'\"\\/]{12,}$";

public static final String SPECIAL_CHAR_REGEX = "^(?=.*[@$!%*?&^~#*()`<>,.|+-_{}:;\'\"\\/])[A-Za-z\\d@$!%*?&^~#*()`<>,.|+-{}:;\'\"\\/]{12,}$";
    

private static boolean checkStrength(String input) {
Pattern upperCasePattern = Pattern.compile(UPPER_CASE_REGEX);
Matcher upperCaseMatcher = upperCasePattern.matcher(input);
int matchCount = 0;
        
        if(upperCaseMatcher.matches()) {
            matchCount++;
            
            if(matchCount >= 2) {
                return true;
            }
        }

if(lowerCaseMatcher.matches()) {
            matchCount++;
            
            if(matchCount >= 2) {
                return true;
            }
        }
        
        if(numberMatcher.matches()) {
            matchCount++;
            
            if(matchCount >= 2) {
                return true;
            }
        }
        
        if(specialCharMatcher.matches()) {
            matchCount++;
            
            if(matchCount >= 2) {
                return true;
            }
        }
        
        return false;
    }

Try this.

static Pattern UPPER = Pattern.compile("[A-Z]");
static Pattern LOWER = Pattern.compile("[a-z]");
static Pattern DIGIT = Pattern.compile("\\d");
static Pattern SPECIAL = Pattern.compile("[!$#%]");

static boolean checkStrength(String password) {
    if (password == null) return false;
    if (password.length() < 12) return false;
    int matchCount = 0;
    if (UPPER.matcher(password).find()) ++matchCount;
    if (LOWER.matcher(password).find()) ++matchCount;
    if (DIGIT.matcher(password).find()) ++matchCount;
    if (SPECIAL.matcher(password).find()) ++matchCount;
    return matchCount >= 2;
}

test cases:

@Test
public void testCheckStrength() {
    assertEquals(false, checkStrength("1234Abcd"));
    assertEquals(false, checkStrength("123456789012"));
    assertEquals(false, checkStrength("HELLOWORLDHELLO"));
    assertEquals(true, checkStrength("HELLOWORLDHELLOhello"));
    assertEquals(true, checkStrength("123456789z12"));
    assertEquals(false, checkStrength("これは日本語の文字列です"));
    assertEquals(true, checkStrength("これはNihongoの文字列です"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM