匹配至少1个大写,1个特殊,4个非序列号,长度为6到14

时间:2014-04-09 12:31:44

标签: php regex

我试图在任何地方匹配至少1个大写字母,1个特殊位置,4个非连续数字,长度为6到14.

我尝试了以下正则表达式:

  

((?=(.*[^A-Za-z0-9])+)(?=(.*[A-Z])+)(?=(.*\d){4,})(?=.*[a-z])+)(?!=.*([\d])\\1\\1\\1\\1).{6,14}

但以下所有字符串都匹配:

abDc#E0%F9$845, 
abDc#E0%F9$8y6, 
abDc#E0981, 
#ab1DcE09w1234  **(4 sequential numbers)**, 
#ab1111DcE09, 
E$ab1Dc098, 
1$Eab1Dc0989, 
T3$s7p4s$123, 
T1234$stPass    **(4 sequential numbers)**

并不匹配:

testpass, 
abDc#E0%F9$8y67 (15 characters), 
t3stpass, 
te$stpass, 
TestPass, 
T3$stpass

我该如何解决?

3 个答案:

答案 0 :(得分:1)

试试这个,请在​​使用前进行测试:

function validate_password($password){

 if(empty($password))
  return false;

 if(strlen($password) < 4 || strlen($password) > 14)
  return false;

 $uppercase_test = preg_match('#[A-Z]+#', $password);

 //include special charactors that you want
 $special_test = preg_match('#[!@$%^&*()_+=-]+#', $password);

 $nonsequencial_number_test = !preg_match('#(\d)\\1{4,}#', $password);

 if($uppercase_test && $special_test && $nonsequencial_number_test){
  return true;
 }

 return false;

}
var_dump(validate_password('abDc#E0%F9$8y6'));

答案 1 :(得分:1)

怎么样:

if (preg_match('/^(?=.*[A-Z])(?=(?:.*\d){4,})(?=.*[!@$%^&*()_+=-]).{6,14}$/', $string)) {
    // remove all non digit
    $tmp = preg_replace('/\D+/', '', $string);
    if (preg_match('/0123|1234|2345|3456|4567|5678|6789|3210|4321|5432|6543|7654|8765|9876|0000|1111|2222|3333|4444|5555|6666|7777|8888|9999/', $string)) {
        echo "Failed\n";
    }
    echo "Pass\n";
} else {
    echo "Failed\n";
}

<强>其中:

(?=.*[A-Z])             : at least one uppercase
(?=(?:.*\d){4,})        : at least 4 digits
(?=.*[!@$%^&*()_+=-])   : at least one special character
.{6,14}                 : from 6 to 14 character long

答案 2 :(得分:0)

使用以下PHP页面来测试正则表达式。

1)strlen检查字符串长度
2)首先preg_match检查一个或多个大写字母
3)第二个preg_match检查至少4个数字
4)第三个preg_match检查4个连续数字(上升和下降)
5)第四个preg_match检查至少一个特殊字符

<?php
if (isset($_GET['string'])) {
    if ($_GET['string'] != '') {
        $string = $_GET['string'];
    } else {
        echo "Empty string.";
    }
}
?>
<pre>
<html>
  <head>
    <title>Regular Expression Tester</title>
  </head>
  <body>
    <form action="" method="get">
      <input type="text" name="string" />
      <input type="submit" value="Send" />
</form>
</pre>
<?php
if (isset($_GET['string'])) {

    if(strlen($string) >= 6 && strlen($string) <= 14) { // length between 6 and 14
        echo "Size between 6 and 14: <b><span style=\"color:green;\">OK</span></b>", "<br>";

        if (preg_match('#[A-Z]+#', $string)) { // one or more uppercase chars
            echo utf8_decode("At least one uppercase: <b><span style=\"color:green;\">OK</span></b>"), "<br>";

            if (preg_match('#(?=.*[0-9]{1}.*[0-9]{1}.*[0-9]{1}.*[0-9]{1})#', $string)) { // it has at least 4 numbers anywhere
                if (!preg_match('#(0123|1234|2345|3456|4567|5678|6789|3210|4321|5432|6543|7654|8765|9876|0000|1111|2222|3333|4444|5555|6666|7777|8888|9999)#', $string)) { // sequential numbers
                    echo utf8_decode("4 non-sequential numbers: <b><span style=\"color:green;\">OK</span></b>"), "<br>";

                    if (preg_match('#[!@$%^&*()_+=-]+#', $string)) { // special chars
                        echo utf8_decode("At least one special character: <b><span style=\"color:green;\">OK</span></b>"), "<br>";
                        echo utf8_decode("<br>$string <b><span style=\"color:green;\">PASSED</span></b>");
                        exit;

                    } else {
                        echo utf8_decode("At least one special character: <b><span style=\"color:red;\">FAILED</span></b>"), "<br>";
                    }

                } else {
                    echo utf8_decode("4 non-sequential numbers: <b><span style=\"color:red;\">FAILED</span></b>"), "<br>";
                }
            } else {
                echo utf8_decode("4 non-sequential numbers: <b><span style=\"color:red;\">FAILED</span></b>"), "<br>";
            }

        } else {
            echo utf8_decode("At least one uppercase: <b><span style=\"color:red;\">FAILED</span></b>"), "<br>";
        }

    } else {
        echo "Size between 6 and 14: <b><span style=\"color:red;\">FAILED</span></b>", "<br>";
    }

    echo utf8_decode("<br>$string <b><span style=\"color:red;\">DIDN'T PASS</span></b>");
}
?>
  <pre>
  </body>
</html>
</pre>

截图:

regex tester 1

regex tester 2

regex tester 3

编辑:但是T1e2$3t4没有通过,所以问题出现在第二个preg_match中。它正在搜索4个数字IN A ROW,而不是4个数字在任何位置。

编辑2:将正则表达式更改为#(?=.*[0-9]{1}.*[0-9]{1}.*[0-9]{1}.*[0-9]{1})#,现在它完全正常工作。

感谢大家! : - )