Java Regex User specific characters to be allowed -
i trying use regex, allow user specific given password.
i tried not working
^[a-za-z0-9@\\#$%&*()_+\\]\\[';:?.,!^-]{"+min_length+","+max_length+"}$ the min_length , max_length database working min , max lenth case, how can give specific uppercase, lowercase, numeric , special characters.
regards pradeep
i'm afraid regexes aren't powerful. find solution using single regex, totally unreadable , unscalable. suggest separate each constraint logic subregex. example:
public static boolean ispasswordvalid(string password) { // 1 3 occurrences of lowercased chars if (!password.matches("(?:[^a-z]*[a-z]){1,3}[^a-z]*")) { return false; } // 2 4 occurrences of uppercased chars if (!password.matches("(?:[^a-z]*[a-z]){2,4}[^a-z]*")) { return false; } // 3 5 occurrences of digits if (!password.matches("(?:[^0-9]*[0-9]){3,5}[^0-9]*")) { return false; } // 4 6 occurrences of special chars (simplified "_", "." or "-") if (!password.matches("(?:[^_.-]*[_.-]){4,6}[^_.-]*")) { return false; } // no other kind of chars, , password length 3 20 if (!password.matches("[a-za-z0-9_.-]{3,20}")) { return false; } return true; }
Comments
Post a Comment