1

Need to set a validation for password where the use case is "Password must require 1 Upper Case, 1 Special Character and at least 6 characters"

Currently using Pattern:

pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}"

However, If I enter password as John@doe - it still asks to enter a digit (number). I presume it is because of (?=.*\d), but if I remove this, then password is accepted without any special character.

How can I use a pattern where it asks for Special Character but not a digit (number)?

mplungjan
  • 169,008
  • 28
  • 173
  • 236

1 Answers1

1

Just an update - following pattern worked

^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[#?!@$%^&*-]).{6,}$

Adding Description/Breakdown:

Earlier (?=.*\d) was consuming both numbers & special characters - not sure why. But separating them as below helps to resolve this.

At least one upper case English letter, (?=.*?[A-Z]) At least one lower case English letter, (?=.*?[a-z]) At least one digit, (?=.*?[0-9]) At least one special character, (?=.*?[#?!@$%^&*-]) Minimum six in length .{6,} (with the anchors)

source: https://stackoverflow.com/a/19605207/10926448