Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 11th, 2012  |  syntax: None  |  size: 2.78 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. String contains at least one digit
  2. int combinations = 0;
  3.       string pass = "!!!AAabas1";
  4.  
  5.       if (pass.matches("[0-9]")) {
  6.           combinations = combinations + 10;
  7.       }
  8.  
  9.       if (pass.matches("[a-z]")) {
  10.           combinations =combinations + 26;
  11.       }
  12.  
  13.       if (pass.matches("[A-Z]")) {
  14.           combinations =combinations + 26;
  15.       }
  16.        
  17. int combinations = 0;
  18.     String pass = "!!AAabas1";
  19.     if (Pattern.compile("[0-9]").matcher(pass).find()) {
  20.         combinations = combinations + 10;
  21.     }
  22.  
  23.     if (Pattern.compile("[a-z]").matcher(pass).find()) {
  24.         combinations = combinations + 26;
  25.     }
  26.  
  27.     if (Pattern.compile("[A-Z]").matcher(pass).find()) {
  28.         combinations = combinations + 26;
  29.     }
  30.        
  31. if (CharMatcher.inRange('0', '9').matchesAnyOf(pass))
  32.   combinations += 10;
  33. if (CharMatcher.inRange('a', 'z').matchesAnyOf(pass))
  34.   combinations += 26;
  35. if (CharMatcher.inRange('A', 'Z').matchesAnyOf(pass))
  36.   combinations += 26;
  37.        
  38. import java.util.regex.Pattern;
  39.  
  40. public class PasswordValidator {
  41.  
  42.     public static void main(String[] args) {
  43.         final PasswordValidator passwordValidator = new PasswordValidator();
  44.         for (String password : new String[] { "abc", "abc123", "ABC123", "abc123ABC", "!!!AAabas1", "гшщз",
  45.                 "гшщзЧСМИ22" }) {
  46.             System.out.printf("Password '%s' is %s%n", password, passwordValidator.isValidPassword(password) ? "ok"
  47.                     : "INVALID");
  48.         }
  49.     }
  50.     private static final Pattern LOWER_CASE = Pattern.compile("\p{Lu}");
  51.     private static final Pattern UPPER_CASE = Pattern.compile("\p{Ll}");
  52.     private static final Pattern DECIMAL_DIGIT = Pattern.compile("\p{Nd}");
  53.  
  54.     /**
  55.      * Determine if a password is valid.
  56.      *
  57.      * <p>
  58.      * A password is considered valid if it contains:
  59.      * <ul>
  60.      * <li>At least one lower-case letter</li>
  61.      * <li>At least one upper-case letter</li>
  62.      * <li>At least one digit</li>
  63.      * </p>
  64.      *
  65.      * @param password
  66.      *            password to validate
  67.      * @return True if the password is considered valid, otherwise false
  68.      */
  69.     public boolean isValidPassword(final String password) {
  70.         return containsDigit(password) && containsLowerCase(password) && containsUpperCase(password);
  71.     }
  72.  
  73.     private boolean containsDigit(final String str) {
  74.         return DECIMAL_DIGIT.matcher(str).find();
  75.     }
  76.  
  77.     private boolean containsUpperCase(final String str) {
  78.         return UPPER_CASE.matcher(str).find();
  79.     }
  80.  
  81.     private boolean containsLowerCase(final String str) {
  82.         return LOWER_CASE.matcher(str).find();
  83.     }
  84.  
  85. }
  86.        
  87. Password 'abc' is INVALID
  88. Password 'abc123' is INVALID
  89. Password 'ABC123' is INVALID
  90. Password 'abc123ABC' is ok
  91. Password '!!!AAabas1' is ok
  92. Password 'гшщз' is INVALID
  93. Password 'гшщзЧСМИ22' is ok