1

I'm trying to make a regex to check a password field, I want to require at least one uppercase letter, one lower case letter and one number.

I also want to allow the user to use special characters, but I don't want special characters to be a requirement.

Is this possible and if so, what regex should I use?

Ewan Roycroft
  • 87
  • 1
  • 1
  • 8
  • (That's not what makes a good password: https://tech.dropbox.com/2012/04/zxcvbn-realistic-password-strength-estimation/) – Rudie Dec 14 '13 at 18:14
  • Please do a search before posting - this question has been asked and answered many times. e.g. In this case search: "JavaScript Password Validation" – ridgerunner Dec 14 '13 at 18:39

2 Answers2

7

If you don't want special characters to be a requirement, there is no point validating it.

This regex should do: /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).*$/

Note that this regex will not validate properly for characters not in the default ascii range of a-zA-Z. So if my password was åäöÅÄÖ1234 it would not be valid for this regex, even though there are upper case and lower case letters.

A possible solution, if this is a requirement, would be to rewrite using unicode properties.

Demo: http://regex101.com/r/uO6jB5

Firas Dib
  • 2,743
  • 19
  • 38
1

From this page:

"Password matching expression. Password must be at least 4 characters, no more than 8 characters, and must include at least one upper case letter, one lower case letter, and one numeric digit."

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$

(note: you'll want to test this to be sure.)

broofa
  • 37,461
  • 11
  • 73
  • 73