preg match for repeating or incremental characters

两盒软妹~` 提交于 2019-12-06 07:38:26

问题


I am using preg match to validate password as:

(preg_match("/^.*(?=.{5,}).*$/", $password)

which accept special characters too.

but for some reason, i need to modify it which should accept

  • only alphanumeric
  • minimum 5 characters long
  • and very important that it must not have any repeating or incremental characters like: aaaa or 12345 or abc123 etc

.


回答1:


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
    }



回答2:


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;
  }
}


来源:https://stackoverflow.com/questions/16251874/preg-match-for-repeating-or-incremental-characters

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