-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.
5
  • See this Link maybe helps you to understand Regex vogella.com/tutorials/JavaRegularExpressions/article.html Commented Oct 20, 2014 at 21:42
  • 1
    What's your question? What have you tried? Where are you stuck? Commented Oct 20, 2014 at 21:43
  • 1
    I wouldn't write a Regex for a simple task as this, but as you haven't even tried... Commented Oct 20, 2014 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. Commented Oct 20, 2014 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. Commented Oct 20, 2014 at 21:49

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

-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.

3 Comments

Using a regex to see if a string's length is >= some number is... interesting.
Yeah, that's not how I would prefer to do it. However, the OP asked for a regex. I don't know whether that was a requirement or whether it was just the first thing that jumped to mind.
(I'm not the one who downvoted you, though.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.