1

I have added a regex which will check for least 8 characters, at least one number, one uppercase letter, one lowercase letter and one special character This is the regex Used

'(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&])[A-Za-zd$@$!%*?&].{8,}'

The above regex works fine for most of the scenarios but when I use

1oB!gb0s5

or

Pass@123

It fails. Can anyone tell me the issue here.

2

1 Answer 1

1

Here is the portion of your regex which actually consumes the input:

[A-Za-zd$@$!%*?&].{8,}

This means that the password must start with one of the characters in the above character class. It also means that a valid password must have nine or more characters, because the class counts for one, and {8,} means 8 or more. So the following would fail because it does not begin with any such character:

1oB!gb0s5

The second example you gave fails for a different reason, because it only has 8 characters:

Pass@123

I don't know exactly what logic you want here. If you just want to ensure that a password has a lowercase, uppercase, number, and special character, then maybe you can remove the leading character class and just stick with the lookaheads:

(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$@$!%*?&]).{8,}

Here is a demo which shows that your two example passwords would pass using the above pattern.

Demo

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

Comments

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.