still2blue
still2blue

Reputation: 193

PHP password validator output

I know there are several threads involving this subject already but my questions is more about the output. I have an array with 10 passwords. They are sent to a function to validate the password. Right now, the function is (somewhat) doing what I need it to, i.e. catching passwords that don't meet the criteria, but it is only catching one thing at a time. I need the function to return EACH criteria that the password did not meet. So for example: "abcd" should return a string stating that "Password is too short, Password did not contain a number, Password did not contain a special character, etc."

How do I add to the string each time an if statement is not met? Thanks so much for any help!

function validatePassword($pwd) {
     if (strlen($pwd) < '8') {
        $invalidPassword = "Your Password Must Contain At Least 8 Characters!";
    }
    elseif(strlen($pwd) > '16') {
        $invalidPassword = "Your Password is too long!";
    }   
    elseif(!preg_match("#[0-9]+#",$pwd)) {
        $invalidPassword = "Your Password Must Contain At Least 1 Number!";
    }
    elseif(!preg_match("#[A-Z]+#",$pwd)) {
        $invalidPassword = "Your Password Must Contain At Least 1 Capital Letter!";
    }
    elseif(!preg_match("#[a-z]+#",$pwd)) {
        $invalidPassword = "Your Password Must Contain At Least 1 Lowercase Letter!";
    }

    return $invalidPassword;
}

$Passwords = array("donkeypass", "password", "Prebyt1na!", "1234", "abcd", "narW1@asndk", "pasS w0rd!", "maK%sh1ft", "mypasswordisthebestpasswordever!23493484023", "sD123#vAr2@y7");
foreach ($Passwords as $value) {
    echo validatePassword($value);
}

Upvotes: 0

Views: 94

Answers (2)

Jim_M
Jim_M

Reputation: 273

In order to keep a list of the failures, we must first turn all the elseif's to if's. Then we are going to collect the errors in an array. When using the [] operator this just means add this to the end of the current array.

Then we will return those results. If the size (count()) is > 0 we will use implode to concatenate the error array into a string separated by commas and output the result.

If no errors are found, we will output a positive result.

   function validatePassword($pwd)
    {
        $invalidPassword = array();
        if (strlen($pwd) < '8') {
            $invalidPassword[] = "Your Password Must Contain At Least 8 Characters!";
        }
        if (strlen($pwd) > '16') {
            $invalidPassword[] = "Your Password is too long!";
        }
        if (!preg_match("#[0-9]+#", $pwd)) {
            $invalidPassword[] = "Your Password Must Contain At Least 1 Number!";
        }
        if (!preg_match("#[A-Z]+#", $pwd)) {
            $invalidPassword[] = "Your Password Must Contain At Least 1 Capital Letter!";
        }
        if (!preg_match("#[a-z]+#", $pwd)) {
            $invalidPassword[] = "Your Password Must Contain At Least 1 Lowercase Letter!";
        }

        return $invalidPassword;
    }

    $Passwords = array("donkeypass", "password", "Prebyt1na!", "1234", "abcd", "narW1@asndk", "pasS w0rd!", "maK%sh1ft", "mypasswordisthebestpasswordever!23493484023", "sD123#vAr2@y7");
    foreach ($Passwords as $value) {
        $return = validatePassword($value);
        if (count($return) > 0) {
            echo "Password Strong Test(s) failed $value " . implode(",", $return) . "<br/>";
        }else{
            echo "Password Strong Test(s) passed $value <br/>";
        }
    }

Upvotes: 3

Michael
Michael

Reputation: 360

I wrote function for you:

function validatePassword($pwd) {
     $messages = array();
     $index = 0;
     if (strlen($pwd) < '8') {
        $messages[$index] = "Your Password Must Contain At Least 8 Characters!";
        $index++;
    }
    if(strlen($pwd) > '16') {
        $messages[$index] = "Your Password is too long!";
        $index++;
    }   
    if(!preg_match("#[0-9]+#",$pwd)) {
        $messages[$index] = "Your Password Must Contain At Least 1 Number!";
        $index++;
    }
    if(!preg_match("#[A-Z]+#",$pwd)) {
        $messages[$index] = "Your Password Must Contain At Least 1 Capital Letter!";
        $index++;
    }
    if(!preg_match("#[a-z]+#",$pwd)) {
        $messages[$index] = "Your Password Must Contain At Least 1 Lowercase Letter!";
        $index++;
    }

    return $messages;
}

Upvotes: 0

Related Questions