0

I am trying to develop a regex pattern for password with following conditions 1) atleast 1 Uppercase character 2) atleast 3 lower case characters 3) atleast 1 digit 4) atleast 1 Special character 5) Minimum length should be 8 characters. This is my javascript function. Can somebody help me with the expression. Thanks

validatePassword : function(password){
        if(this.isEmpty(password))
        {
            return false;
        }
        var regex = /^(?=.*[A-Z])(?=.*[!@#$&*])(?=.*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$/;
        if(!regex.test(password))
        {
            return false
        }
        return true
    }
2
  • 1
    Why the specific requirement of at least 3 lowercase characters? You're complicating it for both you as developer and the user who will have to pick a password matching your very specific rules. Refer to this answer, where you can find very similar regex (at least 1 lowercase, uppercase, digit special character and length of 8+). Also some simplifications, you don't need to check for empty as this regex will fail in that case, and you can just use return regex.text(password). Commented Apr 30, 2016 at 2:13
  • @marko.. thanks...that helped me Commented Apr 30, 2016 at 2:21

1 Answer 1

1
/^(?=.*?[A-Z])(?=(?:.*[a-z]){3})(?=.*?[0-9])(?=.*?[^\w\s]).{8,}$/

This regex will enforce these rules:

• At least one upper case
• At least three lower case
• At least one digit
• At least one special character
• Minimum 8 in length

JSFIDDLE

Sign up to request clarification or add additional context in comments.

5 Comments

Wouldn't the {2,} be at least 2 lowercase characters?
for atleast 3, it should be {3,}.. isn't it?
you want to use .* instead of .? as you want the lookahead to match any number of characters before the lookahead specific match. The lower case match should be (?=(?:.*[a-z]){3}).
@chan if you used {3,} lower case validation must be 4 characters instead, you can see the outcome above link.
@timolawl thanks for another answer. I updated the code

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.