-1

I am writing a Java program in Eclipse that checks whether the password entered meets the terms and conditions and is a valid password or not.

Terms and Conditions are:

  1. Password must contain at least one Uppercase Letter (any position)
  2. Password must contain at least one LowerCase Letter (any position)
  3. Password must contain a Number (any position)
  4. Password must be at least 6 characters long.
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • See this Link maybe helps you to understand Regex http://www.vogella.com/tutorials/JavaRegularExpressions/article.html – JHDev Oct 20 '14 at 21:42
  • 1
    What's your question? What have you tried? Where are you stuck? – Greg Hewgill Oct 20 '14 at 21:43
  • 1
    I wouldn't write a Regex for a simple task as this, but as you haven't even tried... – Smutje Oct 20 '14 at 21:44
  • 2
    I'm with Smutje on both accounts. But here's a hint: rather than doing it with a regex, why not just loop over the chars in the password, and as you go record whether you find an upper case, a lower case, and a number. Then your answer is just `foundUpper && foundLower && foundNumber && string.length() >= 6`. – yshavit Oct 20 '14 at 21:48
  • What are you trying to do here? Are you trying to learn how to write regex, or are you trying to write good code? If the former, you can probably make regex do this, but it's a terrible way to do it. The code you generate this way won't be easy to read, understand, maintain, fix, or extend, and all of those are crucial for good programming. If you want to write this well, write functions to validate each rule, and test on the series of rules. If all pass, the password passes. – Jon Kiparsky Oct 20 '14 at 21:49

2 Answers2

2

I would suggest you avoid a complicated regex, and just iterate the characters in the password. Test each condition against with its' corresponding Character utility method like,

public static boolean isPasswordValid(String pw) {
    if (pw != null && pw.length() > 5) {
        boolean oneUpper = false;
        boolean oneLower = false;
        boolean oneDigit = false;
        for (char ch : pw.toCharArray()) {
            if (Character.isUpperCase(ch)) {
                oneUpper = true;
            } else if (Character.isLowerCase(ch)) {
                oneLower = true;
            } else if (Character.isDigit(ch)) {
                oneDigit = true;
            }
        }
        return oneUpper && oneLower && oneDigit;
    }
    return false;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
-1

You can accomplish your requirements using the following regular expressions:

  1. [A-Z] will match if there is an uppercase letter anywhere in the string
  2. [a-z] will match if there is a lowercase letter anywhere in the string
  3. [0-9] will match if there is a digit anywhere in the string
  4. .{6} will match if there are six (or more) characters in a row

To match all your requirements, check to see whether each of the above match and && the results together.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285