preg match for repeating or incremental characters

眉间皱痕 提交于 2019-12-04 14:32:35
if (preg_match(
    '%^           # Start of string
    (?!.*(.)\1)   # Assert no repeated characters
                  # Assert no sequential digits/characters
    (?!.*(?:01|12|23|34|45|56|67|78|89|90|
            ab|bc|cd|de|ef|fg|gh|hi|ij|jk|
            kl|lm|mn|no|op|pq|qr|rs|st|tu|
            uv|vw|wx|xy|yz))
    [a-z0-9]{5,}  # Match at least five alnum characters
    $             # End of string%ix', 
    $password)) {
    # Successful match
    }

expanding on @TimPietzcker and he should take all credit. So be sure to upvote his answer. But I needed this:

no triple repeated characters

no triple incremental characters / digits

because double incremental characters is odd, you won't even notice you did it.

E.g. test4x3x6 should work right? wrong, st is an incremental character. This will just drive your users crazy.

So I used this:

function is_password_strong($password){
  $preg_res = preg_match(
      '%^           # Start of string
    (?!.*(.)\1{2})   # Assert no triple repeated characters
                  # Assert no sequential digits/characters
    (?!.*(?:012|123|234|345|456|567|678|789|890|
            abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|
            klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|
            uvw|vwx|wxy|xyz))
    .{8,}  # Match at least 8 characters
    $             # End of string%ix',
      $password);

  if ($preg_res) {

    return true;
  }else{
    return false;
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!