-4

I need regex for password validation. I have two regexs.

1) String regexOne_must_contain = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\\w\\s]).{6,15}$"; 2) String regexTwo_must_not_contain = "[%,&,+,\\,\\s,\"]";

I want to combine this regex in one regex to validate password. The Password length should be of minimum 6 characters, must contains at least one special character(_.!@$*=-?), one upper case, one lower case and one numeric character

Karan
  • 1,048
  • 2
  • 20
  • 38
  • See [Reference - Password Validation](https://stackoverflow.com/questions/48345922/reference-password-validation/48346033#48346033) – ctwheels Jan 14 '21 at 15:05

1 Answers1

1

Change the final part from matching . 6-15 times, to matching anything but those prohibited characters, via [^%&+\\\s"].

You also need to properly test for the special characters - put them into a character set. [^\\w\\s] isn't enough:

^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[_.!@$*=-?])[^%&+\\\s"]{6,15}$

https://regex101.com/r/3eiyMD/1

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320