Skip to content

Instantly share code, notes, and snippets.

@JonghyuKim
Last active August 29, 2015 14:20
Show Gist options
  • Save JonghyuKim/68ed5733a3c34d95fd9e to your computer and use it in GitHub Desktop.
Save JonghyuKim/68ed5733a3c34d95fd9e to your computer and use it in GitHub Desktop.
[Android]Regex Password Number + Char + Special
public static int checkPassword(String password){
String charRegex = "a-zA-Z";
String numRegex = "0-9";
String specialRegex = "\\~`!@#\\$%\\^&\\*\\(\\)\\-+\\|\\{\\}\\[\\];\\\\:'\"<>\\?,./_=";
//Number Only, Char Only, Special Only
if(Pattern.matches(String.format("^[%s]*$",charRegex), password) ||
Pattern.matches(String.format("^[%s]*$",numRegex), password) ||
Pattern.matches(String.format("^[%s]*$",specialRegex), password)){
return -1;
}
//Number + Char + Special
//Number + Char
if(Pattern.matches(String.format("^[%s%s%s]*$", charRegex,numRegex,specialRegex), password)){
//Number + Special, Char + Special remove
if(Pattern.matches(String.format("^[%s%s]*$", charRegex,specialRegex), password)||
Pattern.matches(String.format("^[%s%s]*$", numRegex,specialRegex), password)){
return -2;
}
return 0;
}
//Number + Char + Not suport special char
if(Pattern.matches(String.format("^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[^%s]).*$",specialRegex), password)){
return -3;
};
//other...
return -4;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment