Speed up development with full-stack environments for every branch.

Learn More

How to validate complex passwords using regular expressions [PHP and PCRE]

2138 Runs 25737 Views 17119 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/*
 * PHP PCRE - How to validate complex passwords using regular expressions
 */

function valid_pass($candidate) {
    if (!preg_match_all('$\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])(?=\S*[\W])\S*$', $candidate))
        return FALSE;
    return TRUE;
}
/*
    Explaining $\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])(?=\S*[\W])\S*$
    $ = beginning of string
    \S* = any set of characters
    (?=\S{8,}) = of at least length 8
    (?=\S*[a-z]) = containing at least one lowercase letter
    (?=\S*[A-Z]) = and at least one uppercase letter
    (?=\S*[\d]) = and at least one number
    (?=\S*[\W]) = and at least a special character (non-word characters)
    $ = end of the string

 */

$password = 'mypassword01';
if(valid_pass($password))
    echo "$password is a valid password<br />";
else echo "$password is NOT a valid password<br />";

$password = '[email protected]';
if(valid_pass($password))
    echo "$password is a valid password<br />";
else echo "$password is NOT a valid password<br />";
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).