1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Password Validation

Discussion in 'PHP' started by 812402, Aug 6, 2011.

  1. #1
    I need a password validation code snippet for my registration form. Here are the rules for the password:
    - At least one uppercase letter
    - At least one lowercase letter
    - At least one number
    - At least one special character
    - Only alphanumeric and !@#$%^&*() characters

    Thanks

    -812402
     
    812402, Aug 6, 2011 IP
  2. dthoai

    dthoai Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    38
    #2
    Following is code snippet which validate password:  

    
    function validatePassword($src) { 
      $valid = true; 
      $upper = false; 
      $lower = false; 
      $digit = false; 
      $special = false; 
      for ($i = 0; $i < strlen($src); $i++) { 
        $co = ord($src{$i}); 
        if ($co >= ord('A') && $co <= ord('Z')) { 
          $upper = true;
        } else if ($co >= ord('a') && $co <= ord('z')) { 
          $lower = true;
        } else if ($co >= ord('0') && $co <= ord('9')) { 
          $digit = true; 
        } else if (strpos("!@#$%^&*()", $src{$i}) != false) { 
          $special = true; 
        } else { 
          $valid = false; 
        } 
      } 
      return $valid && $upper && $lower && $digit && $special; 
    } 
    
    PHP:
     
    dthoai, Aug 6, 2011 IP
  3. 812402

    812402 Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you!
     
    812402, Aug 6, 2011 IP
  4. 812402

    812402 Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How do you work it?
     
    812402, Aug 7, 2011 IP
  5. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #5
    For this code you would need to post the data to a page with this function on it and call it from there. If you are looking to validate prior to submitting the form then you would need to use javascript instead of php.
     
    Thorlax402, Aug 7, 2011 IP
  6. sinha.sidhi

    sinha.sidhi Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I am mention regex below....

    Assert a string is 8 or more characters:
    (?=.{8,})

    Assert a string contains at least 1 lowercase letter (zero or more characters followed by a lowercase character):
    (?=.*[a-z])

    Assert a string contains at least 1 uppercase letter (zero or more characters followed by an uppercase character):
    (?=.*[A-Z])

    Assert a string contains at least 1 digit:
    (?=.*[\d])

    Assert a string contains at least 1 special character:
    (?=.*[\W])

    Assert a string contains at least 1 special character or a digit:
    (?=.*[\d\W])
     
    sinha.sidhi, Aug 29, 2011 IP