开发者

Regex to check string for unique characters and prohibited characters

I'm writing a password validation regex and I've managed to get 80-90% of the way there but can't incorporate the last two pieces I need and I'm sick of beating my head against the wall so that's where you guys come in ;)

Here is my expression so far:

^(?!.*(.)\1{3}).*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\Q~!@#$%^&*()-_=+[]{}|;:,.<>/?\E]).*$

so in order I have the following rules:

  1. (?!.*(.)\1{3}) - no more than 3 of the same character in sequence
  2. .*(?=.{8,}) - string must be a minimum of 8 characters
  3. (?=.*\d) - must contain at least one digit
  4. (?=.*[a-z]) - must contain at least one lower case letter
  5. (?=.*[A-Z]) - must contain at least one upper case letter
  6. (?=.\*[\Q~!@#$%^&*()-_=+[]{}|;:,.<>/?\E]) - must contain at least one of these special characters

I need to add two more restrictions

1) no character other than an alphanumeric or one of my special characters may appear in the string. So I think I have the basic expression correct:

^([\w\Q~!@#$%^&*()-_=+[]{}|;:,.<>/?\E]*)$

but when I try to add that into my overall expression it doesn't work or it screws up one of my other conditions, so I'开发者_运维技巧m not sure what I'm doing wrong

2) the string MUST contain 4 unique characters. I cant figure this one out at all.

thanks in advance for any help you can provide


Try this one. (I removed a couple of .*s which aren't needed and removed the minimum of 8 chars because that can be incorporated in the final piece.)

^
(?!.*(.)\1{3})
(?=.*\d)
(?=.*[a-z])
(?=.*[A-Z])
(?=.*[\Q~!@#$%^&*()-_=+[]{}|;:,.<>/?\E])
[\w\Q~!@#$%^&*()-_=+[]{}|;:,.<>/?\E]{8,}
$

Also, your last rule:

the string MUST contain 4 unique characters.

Is already checked for, because you are requesting one digit, one upper, one lower, and one special = four different classes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜