tags:

views:

36

answers:

1

Hi,

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

Thank you, -tesh

+3  A: 
(?=.*\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.

Bennor McCarthy
Wow hat was quick. I just tested it. It works great. Thanks Bennor. Also thanks for the explanation.
Hitesh Rana