开发者

Regular expression to validate current DoD Password Standards

Here are two different ones that are not working yet.

One:

^[a-zA-Z0-9\{\}:;,.?\-=+|<>!@#$%^&()\[\]\/\\)]{15,}

Two:

(?=^.{15,}$)(?=.*\d{开发者_StackOverflow中文版2,})(?=.*\W+)(?![.\n])(?=.*[A-Z]{2,})(?=.*[a-z]{2,}).*$

Rules:

Passwords must be at least 15 characters in length.

Passwords must contain at least 2 character(s) from each of these 4 categories:

  • uppercase alphabetical A-Z
  • lowercase alphabetical a-z
  • numeric 0-9
  • special characters {}:;,.?-=+|<>!@#$%^&()[]/\

Passwords must not contain any spaces.

Thanks for any help.

---- Edited ----

Some Samples to try

2wsx@WSX3edc#EDC

3edc#EDC4rfv$RFV

nhy6NHY^mju7MJU&

1qaz!QAZ2wsx@WSX

I have tested them at http://www.gskinner.com/RegExr/ and I am not getting them to work.

I feel like some of the below examples should work but they don't or I must be doing something wrong.


Either check these regexes in one after another or create one big look-ahead-based expression from them.

Rules:

  • Passwords must be at least 15 characters in length.
^.{15,}$

Passwords must contain at least 2 character(s) from each of these 4 categories:

  • uppercase alphabetical (A-Z)
^(.*?[A-Z]){2}
  • lowercase alphabetical (a-z)
^(.*?[a-z]){2}
  • numeric (0-9)
^(.*?[0-9]){2}
  • special characters ({}:;,.?-=+|<>!@#$%^&()[]/)
^(.*?[{}:;,.?\-=+|!@#$%^&()\[\]/]){2}

Passwords must not contain any spaces.

Why? But if you must, change the first expression to

^\S{15,}$


Why do it all in one regex? Do something like:

if( length($password) >= 15 &&
    ($password =~ y/A-Z//) >= 2 &&
    ($password =~ y/a-z//) >= 2 &&
    ($password =~ y/0-9//) >= 2 &&
    ($password =~ y/{}:;,.?\-=+|<>!@#%^&()\[\]///) >= 2 &&
    $password =~ /^\S+$/ ) {

  print "password validates!\n";
} else {
  print "password doesn't validate!\n";
}

This way when you have to add more criteria, it's easy to do. This is also a hell of a lot more readable and maintainable than putting it all in one giant regex.


I found the answer.... for JavaScript... Thanks for all the help.

This one works.

^((?=(.*[\d]){2,})(?=(.*[a-z]){2,})(?=(.*[A-Z]){2,})(?=(.*[^\w\d\s]){2,})).{15,}$


^(?!.* )(?=.{15})(?=.*[A-Z].*[A-Z])(?=.*[a-z].*[a-z])(?=.*[0-9].*[0-9])(?=.*[{}:;,.?\-=+|<>!@#$%\^&()[\]/\\].*[{}:;,.?\-=+|<>!@#$%\^&()[\]/\\])

My Regex should work even for separated chars. So for example Ab0%Ab0%0000000000000 (you see, there isn't an AA, the two A are separated). I have escape the minimum number of characters (only ^ and ])

  • ^ Beginning of the string

  • (?!.* ) Not (any character repeated * times followed by a space)

  • (?=.{15}) Any character for 15 times (as it's written, even if there are other characters after the 15th it won't change anything)

  • (?=.*[A-Z].*[A-Z]) Any character repeated * times, an Upper Case letter, any character repeated * times, an Upper Case letter

  • (?=.*[a-z].*[a-z]) Any character repeated * times, a Lower Case letter, any character repeated * times, a Lower Case letter

  • (?=.*[0-9].*[0-9]) Any character repeated * times, a Digit, any character repeated * times, a Digit

  • (?=.*[{}:;,.?\-=+|<>!@#$%\^&()[\]/\\].*[{}:;,.?\-=+|<>!@#$%\^&()[\]/\\]) Any character repeated * times, a Symbol, any character repeated * times, a Symbol

All the subexpression are zero-width look-ahead (or zero-width negative look-ahead) and they start all at the first character, so they are all in "and" and they must be all true.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜