sohil
sohil

Reputation: 415

Regex to validate password

I need a regex to validate a password. The constraints are :

-Maximum Embedded Spaces: 0

-Minimum Length: 8

-Maximum Length: 8

-Must not contain letters: Q, q, Z, z

-Maximum Occurrences of one Character/Number: 2

-Maximum Repetitive Character/Number: 2

-Maximum Sequential Character: 3

-Maximum Special Character: 0

-Minimum Alphabet: 1

-Minimum Numbers: 1

Not sure how I should go about it, whether to take the negative or positive approach.

Upvotes: 2

Views: 276

Answers (1)

Ro Yo Mi
Ro Yo Mi

Reputation: 15000

Description

^(?!.*?([a-z0-9])(?:.*?\1){2})(?=.*?[a-z])(?=.*?[0-9])(?!.*?(?:0(?:123|987)|1234|2345|3(?:456|210)|4(?:567|321)|5(?:678|432)|6(?:789|543)|7(?:890|654)|8765|9876))(?!.*?(?:abcd|bcde|cdef|d(?:efg|cba)|e(?:fgh|dcb)|f(?:ghi|edc)|g(?:hij|fed)|h(?:ijk|gfe)|i(?:jkl|hgf)|j(?:klm|ihg)|k(?:lmn|jih)|l(?:mno|kji)|m(?:nop|lkj)|nmlk|onml|ponm|rstu|stuv|tuvw|u(?:vwx|tsr)|v(?:wxy|uts)|w(?:vut)|xwvu|yxwv))[a-pr-y0-9]{8}$

Regular expression visualization

This regular expression is split into components that will do the following:

  1. ^[a-pr-y0-9]{8}$
    • Allow no spaces
    • Require 8 characters, no more no less
    • Not allow letters: Q, q, Z, z
    • Allow zero special characters
  2. (?!.*?([a-z0-9])(?:.*?\1){2})
    • Allow any character to appear at most 2 times anywhere in the string
    • (?=.*?[a-z])
    • Require a minimum of 1 letter
  3. (?=.*?[0-9])
    • Require a minimum of 1 digit
  4. (?!.*?(?:0(?:123|987)|1234|2345|3(?:456|210)|4(?:567|321)|5(?:678|432)|6(?:789|543)|7(?:890|654)|8765|9876))(?!.*?(?:abcd|bcde|cdef|d(?:efg|cba)|e(?:fgh|dcb)|f(?:ghi|edc)|g(?:hij|fed)|h(?:ijk|gfe)|i(?:jkl|hgf)|j(?:klm|ihg)|k(?:lmn|jih)|l(?:mno|kji)|m(?:nop|lkj)|nmlk|onml|ponm|rstu|stuv|tuvw|u(?:vwx|tsr)|v(?:wxy|uts)|w(?:vut)|xwvu|yxwv))

    • Allow upto 3 sequential characters, abc is ok, but abcd is bad
    • Allow upto 3 reverse sequential characters, cba is ok, but dcba is bad

Note: This expression requires the use of the case insensitive flag inorder to match upper and lower case values.

Example

Live Demo

https://regex101.com/r/gL9jN5/4

Sample text

12345678
asfkd2ls
abcd12js
ibicid13
aaaafd212
fdadms1

Sample Matches

asfkd2ls

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
    (                        group and capture to \1:
----------------------------------------------------------------------
      [a-z0-9]                 any character of: 'a' to 'z', '0' to
                               '9'
----------------------------------------------------------------------
    )                        end of \1
----------------------------------------------------------------------
    (?:                      group, but do not capture (2 times):
----------------------------------------------------------------------
      .*?                      any character except \n (0 or more
                               times (matching the least amount
                               possible))
----------------------------------------------------------------------
      \1                       what was matched by capture \1
----------------------------------------------------------------------
    ){2}                     end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
    [a-z]                    any character of: 'a' to 'z'
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
    [0-9]                    any character of: '0' to '9'
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      0                        '0'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        123                      '123'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        987                      '987'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      1234                     '1234'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      2345                     '2345'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      3                        '3'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        456                      '456'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        210                      '210'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      4                        '4'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        567                      '567'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        321                      '321'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      5                        '5'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        678                      '678'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        432                      '432'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      6                        '6'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        789                      '789'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        543                      '543'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      7                        '7'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        890                      '890'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        654                      '654'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      8765                     '8765'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      9876                     '9876'
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
    (?:                      group, but do not capture:
----------------------------------------------------------------------
      abcd                     'abcd'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      bcde                     'bcde'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      cdef                     'cdef'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      d                        'd'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        efg                      'efg'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        cba                      'cba'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      e                        'e'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        fgh                      'fgh'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        dcb                      'dcb'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      f                        'f'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        ghi                      'ghi'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        edc                      'edc'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      g                        'g'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        hij                      'hij'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        fed                      'fed'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      h                        'h'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        ijk                      'ijk'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        gfe                      'gfe'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      i                        'i'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        jkl                      'jkl'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        hgf                      'hgf'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      j                        'j'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        klm                      'klm'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        ihg                      'ihg'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      k                        'k'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        lmn                      'lmn'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        jih                      'jih'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      l                        'l'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        mno                      'mno'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        kji                      'kji'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      m                        'm'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        nop                      'nop'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        lkj                      'lkj'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      nmlk                     'nmlk'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      onml                     'onml'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      ponm                     'ponm'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      rstu                     'rstu'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      stuv                     'stuv'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      tuvw                     'tuvw'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      u                        'u'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        vwx                      'vwx'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        tsr                      'tsr'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      v                        'v'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        wxy                      'wxy'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        uts                      'uts'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      w                        'w'
----------------------------------------------------------------------
      (?:                      group, but do not capture:
----------------------------------------------------------------------
        vut                      'vut'
----------------------------------------------------------------------
      )                        end of grouping
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      xwvu                     'xwvu'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      yxwv                     'yxwv'
----------------------------------------------------------------------
    )                        end of grouping
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  [a-pr-y0-9]{8}           any character of: 'a' to 'p', 'r' to 'y',
                           '0' to '9' (8 times)
----------------------------------------------------------------------
  $                        before an optional \n, and the end of a
                           "line"
----------------------------------------------------------------------

Upvotes: 3

Related Questions