1

I have a password field where I want to have atleast one special character, atleast one uppercase character and atleast one numeric digit. Can anyone help me with the regex pattern please.

Thank You

2
  • 4
    I dont understand why this question has been upvoted! I cant see any effort from the OP's side. Commented Jul 17, 2012 at 9:26
  • 1
    Do you really want to enforce this? People will just add an uppercase at the start, and a 1$ at the end. Not sure enforcing this actually increases security. Password1$ isn't any more secure than password. Commented Jul 17, 2012 at 9:27

1 Answer 1

4
Pattern pwPattern = Pattern.compile("(?=.*[@#$!\"&])(?=.*[A-Z]).*\\d.*");

Add more special characters inside the first brackets if you want to allow more, I just added a few.

Use it like this:

if (pwPattern.matcher(passWordToTest).matches()) {
  ...
}
Sign up to request clarification or add additional context in comments.

7 Comments

Doesn't this have to end in a digit?
+1 But for me it also works without .* at the end even when the pw does not end with a digit.
@FabianBarney: Not in Java when using .matches().
@user1530678: If you want to insert ^ into the character class, do it at the end of the class; if you insert -, do it at the start. Otherwise they change the meaning of what's in the class.
@TimPietzcker Ah ok, I used .find() in my test.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.