-3
@"^(?=.*[0-9]+.*)(?=.*[a-zA-Z]+.*)[0-9a-zA-Z]{6,}$"

I am using this regular expression for password validation which gives one upper case, one lowercase and a number. But what I want is a special character in it but it should optional but above mentioned must be mandatory.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
praveen kumar
  • 141
  • 10

1 Answers1

0

This will allow these special characters: - (hyphen), * (asterisk) and _ (underscore).

^(?=[-_*]*)(?=.*[0-9]+.*)(?=.[a-zA-Z]+.)[-*_0-9a-zA-Z]{6,}$

If you want to add your own special characters, add them into this part of the regex [-*_0-9a-zA-Z] (inside the square bracket)

Hoang Tran
  • 296
  • 1
  • 6