SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,764
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Need help with Password Regex

    How do I modify this so it just requires a minimum Password length of 8 characters, but no maximum?

    PHP Code:
        if (preg_match(    (?=.{8,15}$)        # Ensure Length is 8-15 Characters
                    
    (?=.*?[A-Z])        # Check for at least 1 Upper-Case
                    
    (?=.*?[a-z])        # Check for at least 1 Lower-Case
                    
    (?=.*?[0-9])        # Check for at least 1 Number
                    
    (?=.*?[\~\`\!\@\#\$\%\^\&\*\(\)\_\-\+\=\{\[\}\]\|\:\;\"\'\<\,\>\.\?\/\\\\])    # Check for at least 1 Special-Character
                ){ 

    Do I just changes things to this...

    PHP Code:
        if (preg_match(    (?=.{8, }$) 
    Thanks,


    Debbie

  2. #2
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,764
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I have been playing around and came up with this...

    PHP Code:
        if (preg_match('~(?=.{8, }$)        # Ensure Length is 8 or more Characters
                    (?=.*?[A-Z])        # Check for at least 1 Upper-Case
                    (?=.*?[a-z])        # Check for at least 1 Lower-Case
                    (?=.*?[0-9])        # Check for at least 1 Number
                                    # Check for at least 1 Special-Character
                    (?=.*?[\~\`\!\@\#\$\%\^\&\*\(\)\_\-\+\=\{\[\}\]\|\:\;\"\'\<\,\>\.\?\/\\\\])~'
    $trimmed['newPass1'])){ 

    ...but it is not working.


    Debbie

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I think you forgot to use the "x" mondifier... "~regex~x"
    x (PCRE_EXTENDED)

    If this modifier is set, whitespace data characters in the pattern are totally ignored except when escaped or inside a character class, and characters between an unescaped # outside a character class and the next newline character, inclusive, are also ignored. This is equivalent to Perl's /x modifier, and makes it possible to include commentary inside complicated patterns. Note, however, that this applies only to data characters. Whitespace characters may never appear within special character sequences in a pattern, for example within the sequence (?( which introduces a conditional subpattern.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,764
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    I think you forgot to use the "x" mondifier... "~regex~x"
    I'm trying to piece together stuff from a simpler Regex that has been working...

    I tried this, but still no go?!

    PHP Code:
        if (preg_match('~(?x)            # Comments Mode
                    ^            # Beginning of String Anchor
                    (?=.{8, }$)    # Ensure Length is 8 or more Characters
                    (?=.*?[A-Z])    # Check for at least 1 Upper-Case
                    (?=.*?[a-z])    # Check for at least 1 Lower-Case
                    (?=.*?[0-9])    # Check for at least 1 Number
                                # Check for at least 1 Special-Character
                    (?=.*?[\~\`\!\@\#\$\%\^\&\*\(\)\_\-\+\=\{\[\}\]\|\:\;\"\'\<\,\>\.\?\/\\\\])
                    $            # End of String Anchor
                    ~'
    $trimmed['newPass1'])){ 

    Debbie

  5. #5
    Non-Member
    Join Date
    Jun 2012
    Posts
    88
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If all you want is a min of 8 alpha-numeric chars then
    PHP Code:
    <?php

    $password 
    "abc33";
    $pattern '/^[\da-zA-Z]{8,}$/';
    $isPasswordValid preg_match($pattern$password);
    echo 
    $isPasswordValid;  //outputs 0 or 1
    ?>

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    The "x" goes at the very end of the regex, after "~". Without it those comments and whitespace are part of the regex.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •