4

I want to validate password .The following are my requirements.

Minimum password length: 8
Minimum number of lower case characters: 1
Minimum number of upper case characters: 1
Minimum number of numeric characters: 1

How to write a regex for this ?

3
  • 2
    My suggestion would be to use a function ValidatePassword instead. You may need to update this to include (For example) the password cannot contain the username. Just a thought. :) Commented Mar 7, 2011 at 6:20
  • 1
    Regex is not the best tool for this job. Commented Mar 7, 2011 at 6:21
  • @Jacob: Usually requests like these come about when a person is using some framework that only allows a regex for validation. Commented Mar 7, 2011 at 6:54

3 Answers 3

4

You can use the following regex:

^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,}$
Sign up to request clarification or add additional context in comments.

1 Comment

I thought ?= (zero-width positive look-ahead) wasn't supported in some of the common client-side languages, but after some research, I'm pleased to see I was wrong. This is the Regex to use.
2

I agree with @Russell, a function is a better choice for password validation. And it's hard to imagine a single Regex handling all these cases. I think you would have to check each one in turn.

Individually, the Regex expressions are:

  • .{8} matches at least 8 characters
  • [a-z] matches a single lowercase character
  • [A-Z] matches a single uppercase character
  • [0-9] matches a digit

That having been said, these would only be useful for client-side checking prior to having the server do in-depth validation.

Comments

0

Please find below regex for your requirements:

(?=^.{8}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$ 

Comments

Your Answer

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