MindGame
MindGame

Reputation: 1251

Regular Expression for password

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

Upvotes: 2

Views: 1177

Answers (1)

Bennor McCarthy
Bennor McCarthy

Reputation: 11675

(?=.*\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.

Upvotes: 5

Related Questions