4

スクリプトから目的の効果が得られません。パスワードに A ~ Z、a ~ z、0 ~ 9、および特殊文字を含めたい。

  • AZ
  • から
  • 0-9 >= 2
  • 特殊文字 >= 2
  • 文字列の長さ >= 8

したがって、ユーザーに少なくとも 2 つの数字と少なくとも 2 つの特殊文字を使用するように強制したいと考えています。私のスクリプトは動作しますが、数字または文字を連続して使用する必要があります。私はそれをしたくありません。たとえば、パスワード testABC55$$ は有効ですが、それは望ましくありません。

代わりに、test$ABC5#8 を有効にしたいと考えています。したがって、基本的に数字/特殊文字は同じまたは差分にすることができます->が、文字列内で分割する必要があります。

PHP コード:

$uppercase = preg_match('#[A-Z]#', $password);
$lowercase = preg_match('#[a-z]#', $password);
$number    = preg_match('#[0-9]#', $password);
$special   = preg_match('#[\W]{2,}#', $password); 
$length    = strlen($password) >= 8;

if(!$uppercase || !$lowercase || !$number || !$special || !$length) {
  $errorpw = 'Bad Password';
4

3 に答える 3

12

Using "readable" format (it can be optimized to be shorter), as you are regex newbie >>

^(?=.{8})(?=.*[A-Z])(?=.*[a-z])(?=.*\d.*\d.*\d)(?=.*[^a-zA-Z\d].*[^a-zA-Z\d].*[^a-zA-Z\d])[-+%#a-zA-Z\d]+$

Add your special character set to last [...] in the above regex (I put there for now just -+%#).


Explanation:

^                              - beginning of line/string
(?=.{8})                       - positive lookahead to ensure we have at least 8 chars
(?=.*[A-Z])                    - ...to ensure we have at least one uppercase char
(?=.*[a-z])                    - ...to ensure we have at least one lowercase char
(?=.*\d.*\d.*\d                - ...to ensure we have at least three digits
(?=.*[^a-zA-Z\d].*[^a-zA-Z\d].*[^a-zA-Z\d]) 
                               - ...to ensure we have at least three special chars
                                    (characters other than letters and numbers)
[-+%#a-zA-Z\d]+                - combination of allowed characters
$                              - end of line/string
于 2012-07-03T21:01:02.340 に答える
1
((?=(.*\d){3,})(?=.*[a-z])(?=.*[A-Z])(?=(.*[!@#$%^&]){3,}).{8,})

test $ ABC5#8は、2桁以上の数字と仕様記号を要求するため、無効です。

A-Z
a-z
0-9 > 2
special chars > 2
string length >= 8
于 2012-07-03T20:58:57.990 に答える