-4

Hi i want to find a regular expression that satisfies these conditions.

(1) Passwords must be atleast 8 characters (2) it must contain atleast an upper, lower case letters, numbers, and special characters. (3) the password does not contain more than two successive identical characters.

I have a regex for the first two points ^(?=.?[A-Z])(?=.?[a-z])(?=.?[0-9])(?=.?[#?!@$%^&*-]).{8,}$

I think this regex for repetivie string is (?!(.)\1{2,})

not sure how can i combine both of them .

please help thank you so much prasanth

prasanth
  • 323
  • 1
  • 2
  • 13

1 Answers1

1

Ok then, this might not be pretty, but seems to do the job:
^(?!.*(.)\1\1)(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[.\-!?§$%&]).{8,}.*$ You can see a working demo here.

The site i linked above gives good explanations about regular expressions. Please try to at least understand what happens here, so you can spot errors that occur when using such an ugly RegEx (and so SO doesn't become a coding service site but a place to learn stuff at).

Note that the "special characters" in this example are .-!?§$%&. If you want any others in there, add them to where these are in the pattern. And don't forget to escape - with a \ in character classes, as they have a special meaning here. Just like in this part: .\-!?§$%&.

bkis
  • 2,530
  • 1
  • 17
  • 31
  • when i tested this > /^(?=.*?[A-Z].*?)(?=.*?[a-z].*?)(?=.*?[0-9].*?)(?=.*?[.\-!?§$%&].*?).{8,}.*$/.test("Passsword123$") it returned true , i was expecting it to be false because 3 repetitive characters in Passsword123$ – prasanth Feb 21 '19 at 09:48
  • Sorry, i forgot that condition... let me check it again – bkis Feb 21 '19 at 09:53
  • @prasanth I edited the example *and* the demo to do what you need. It doesn't allow **more than 2** repetitive characters, now. – bkis Feb 21 '19 at 10:02