2

I'm not really good at regular expressions. I need to do the following to validate if a password, entered by the user, is correct or not.

Criteria:

  1. Must contain at least one number
  2. Must contain at least one letter from A-Z or a-z (case does not matter as long as they enter is a letter).
  3. The password must be a minimum of 8 characters
2
  • Knowning the type of regexp (POSIX, PCRE) or at least your platform/tool/programming language might help people willing to reply.
    – Dummy00001
    Aug 15, 2010 at 23:23
  • I'm sorry, I did not know there was a difference. I'm doing the validation for the password in C#. Bennor's reply helped, thanks for replying back.
    – MindGame
    Aug 15, 2010 at 23:33

1 Answer 1

5
(?=.*\d)(?=.*[A-Za-z]).{8,}

The first part ((?=.*\d)) searches for at least one number, the second part ((?=.*[A-Za-z])) searches for at least one letter, and the last part (.{8,}) ensures it's at least 8 characters long.

You might want to put an upper limit on the length of the password like this:

^(?=.*\d)(?=.*[A-Za-z]).{8,30}$

The 30 in that spot limits it to 30 characters in length, and the ^ and $ anchor it to the start and end of the string.

1
  • Wow hat was quick. I just tested it. It works great. Thanks Bennor. Also thanks for the explanation.
    – MindGame
    Aug 15, 2010 at 23:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.