Password Regular Expression Pattern

((?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[@#$%]).{6,20})

Description

( # Start of group
(?=.\d) # must contains one digit from 0-9
(?=.
[a-z]) # must contains one lowercase characters
(?=.[A-Z]) # must contains one uppercase characters
(?=.
[@#$%]) # must contains one special symbols in the list “@#$%”
. # match anything with previous condition checking
{6,20} # length at least 6 characters and maximum of 20
) # End of group

?= – means apply the assertion condition, meaningless by itself, always work with other combination

Whole combination is means, 6 to 20 characters string with at least one digit, one upper case letter, one lower case letter and one special symbol (“@#$%”). This regular expression pattern is very useful to implement a strong and complex password.

P.S The grouping formula order is doesn’t matter.
1. Java Regular Expression Example
PasswordValidator.java

package com.ltutorial.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PasswordValidator{

private Pattern pattern;
private Matcher matcher;

private static final String PASSWORD_PATTERN =
“((?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[@#$%]).{6,20})”;

public PasswordValidator(){
pattern = Pattern.compile(PASSWORD_PATTERN);
}

/**
* Validate password with regular expression
* @param password password for validation
* @return true valid password, false invalid password
*/
public boolean validate(final String password){

matcher = pattern.matcher(password);
return matcher.matches();

}
}

  1. Password that match:

  2. [email protected]

  3. ltYOn12$
  4. Password that doesn’t match:

  5. [email protected] , too short, minimum 6 characters

  6. [email protected] , uppercase characters is required
  7. ltuTorial12* , special symbol “*” is not allow here
  8. ltutoriaL$$, digit is required
  9. LTUTORIAL12$ , lower case character is required
  10. Unit Test – PasswordValidator

Unit test with TestNG.
PasswordValidatorTest.java

package com.ltutorial.regex;

import org.testng.Assert;
import org.testng.annotations.*;

/**
* Password validator Testing
* @author ltutorial
*
*/
public class PasswordValidatorTest {

private PasswordValidator passwordValidator;

@BeforeClass
public void initData(){
passwordValidator = new PasswordValidator();
}

@DataProvider
public Object[][] ValidPasswordProvider() {
return new Object[][]{
{new String[] {
[email protected]”, “ltuTOr12$”,
}}
};
}

@DataProvider
public Object[][] InvalidPasswordProvider() {
return new Object[][]{
{new String[] {
[email protected]”,”[email protected]”,”ltutoriaL12*”,
“ltutoriaL$$”,”LTUTORIAL12$”
}}
};
}

@Test(dataProvider = “ValidPasswordProvider”)
public void ValidPasswordTest(String[] password) {

for(String temp : password){
boolean valid = passwordValidator.validate(temp);
System.out.println(“Password is valid : ” + temp + ” , ” + valid);
Assert.assertEquals(true, valid);
}

}

@Test(dataProvider = “InvalidPasswordProvider”,
dependsOnMethods=”ValidPasswordTest”)
public void InValidPasswordTest(String[] password) {

for(String temp : password){
boolean valid = passwordValidator.validate(temp);
System.out.println(“Password is valid : ” + temp + ” , ” + valid);
Assert.assertEquals(false, valid);
}
}
}

  1. Unit Test – Result

Password is valid : [email protected] , true
Password is valid : ltuTOrial12$ , true
Password is valid : [email protected] , false
Password is valid : [email protected] , false
Password is valid : ltutoriAl12* , false
Password is valid : ltutoriaL$$ , false
Password is valid : LTUTORIAL12$ , false
PASSED: ValidPasswordTest([Ljava.lang.String;@1d4c61c)
PASSED: InValidPasswordTest([Ljava.lang.String;@116471f)

===============================================
com.ltutorial.regex.PasswordValidatorTest

Tests run: 2, Failures: 0, Skips: 0

===============================================
ltutorial

Total tests run: 2, Failures: 0, Skips: 0

2 thoughts on “Validate password with regular expression

Leave a Reply

Your email address will not be published. Required fields are marked *