Last active
January 29, 2022 20:07
-
-
Save mayorJAY/a165ae97bd8084cc58f405e7522563c8 to your computer and use it in GitHub Desktop.
Password Validation Regex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PasswordValidationTest { | |
private Pattern pattern; | |
@Before | |
public void setUp() { | |
String regex = "^(((?=.*[a-z])(?=.*[A-Z])(?=.*\\d))|((?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*#?&]))|((?=.*[a-z])(?=.*[A-Z])(?=.*[@$!%*#?&]))|((?=.*[a-z])(?=.*\\d)(?=.*[@$!%*#?&])))(?=\\S+$).{8,}$"; | |
pattern = Pattern.compile(regex); | |
} | |
@Test | |
public void test_first_valid_password_example() { | |
//Number, upper case, lower case in no particular order | |
String password = "13Edlpouhgfdcfvgbqb"; | |
Assert.assertTrue(matchPassword(password)); | |
} | |
@Test | |
public void test_second_valid_password_example() { | |
//Lower case, number, special character in no particular order | |
String password = "dfgjrwerdvfbghjhgfdxc90@"; | |
Assert.assertTrue(matchPassword(password)); | |
} | |
@Test | |
public void test_third_valid_password_example() { | |
//Upper case, number, lower case in no particular order | |
String password = "POIIUHGFBGNHUYF2df"; | |
Assert.assertTrue(matchPassword(password)); | |
} | |
@Test | |
public void test_fourth_valid_password_example() { | |
//Upper case, lower case, number, special character in no particular order | |
String password = "KJSrtyhgh!58E543561#"; | |
Assert.assertTrue(matchPassword(password)); | |
} | |
@Test | |
public void test_invalid_password_example() { | |
String password = "TGsayhtgfdcsxrbfdvc"; | |
Assert.assertFalse(matchPassword(password)); | |
} | |
//Utility function | |
private boolean matchPassword(String password) { | |
Matcher matcher = pattern.matcher(password); | |
return matcher.matches(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment