Password

Hi all,
I wrote this code. it’s working but I want to add some conditions such as passwords to be a minimum of 8 characters,A password must contain at least one digit, at least one uppercase letter, and at least one lowercase letter. Anyone can help me.

[php]// Check for a password and match against the confirmed password:
if (!empty($_POST[‘pass1’])) {
if ($_POST[‘pass1’] != $_POST[‘pass2’]) {
$errors[] = ‘Your password did not match the confirmed password.’;
} else {
$p = mysqli_real_escape_string($dbc, trim($_POST[‘pass1’]));
}
} else {
$errors[] = ‘You forgot to enter your password.’;
}
[/php]

What have you tried?

Here’s a simplified version of a validation function that I use:
[php]<?php
$errors = [‘validPassword’ => true, ‘matchPassword’ => true]; // Set all errors parameters initially to true:

/*

  • Passing password and verify passord to function, plus
  • passing errors array by reference.
    /
    function passwordCheck($password, $verify, &$errors) {
    /

    *
    • Explaining !preg_match_all(’$\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])(?=\S*[\W])\S*$’, $password)
    • $ = 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 one uppercase letter
    • (?=\S*[\d]) = and at least one number
    • (?=\S*[\W]) = and at least a special character (non-word character)
    • $ = end of the string:
    /
    if (!preg_match_all(’$\S
    (?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])(?=\S*[\W])\S*$’, $password)) {
    $errors[‘validPassword’] = false; // False means it’s an invalid password:
    } else {
    $errors[‘validPassword’] = true;
    }
    if ($password != $verify) {
    $errors[‘matchPassword’] = false; // False means passwords don’t match:
    } else {
    $errors[‘matchPassword’] = true;
    }
    }

$password = “Icsan2016!”;
$verifyPassword = “Icsan2016!”;

passwordCheck($password, $verifyPassword, $errors);

if ($errors) {
echo “Everything checks out OK!
\n”;
echo “

” . print_r($errors,1) . “
\n”;
} else {
echo “There is an error somewhere!
\n”;
echo “
” . print_r($errors,1) . “
\n”;
}

if ($errors[‘validPassword’]) {
echo “

Password is valid!

\n”;
} else {
echo “

Password is invalid!!!

\n”;
}

if ($errors[‘matchPassword’]) {
echo “

Both passwords match!

\n”;
} else {
echo “

Passwords do not match!!!

\n”;
}

$password = “Icsan2016a”;
$verifyPassword = “Icsan2016!”;

passwordCheck($password, $verifyPassword, $errors);

if ($errors) {
echo “Everything checks out OK!
\n”;
echo “

” . print_r($errors,1) . “
\n”;
} else {
echo “There is an error somewhere!
\n”;
echo “
” . print_r($errors,1) . “
\n”;
}

if ($errors[‘validPassword’]) {
echo “

Password is valid!

\n”;
} else {
echo “

Password is invalid!!!

\n”;
}

if ($errors[‘matchPassword’]) {
echo “

Both passwords match!

\n”;
} else {
echo “

Passwords do not match!!!

\n”;
}

$password = “Icsan2016!”;
$verifyPassword = “Icsan2016!s”;

passwordCheck($password, $verifyPassword, $errors);

if ($errors) {
echo “Everything checks out OK!
\n”;
echo “

” . print_r($errors,1) . “
\n”;
} else {
echo “There is an error somewhere!
\n”;
echo “
” . print_r($errors,1) . “
\n”;
}

if ($errors[‘validPassword’]) {
echo “

Password is valid!

\n”;
} else {
echo “

Password is invalid!!!

\n”;
}

if ($errors[‘matchPassword’]) {
echo “

Both passwords match!

\n”;
} else {
echo “

Passwords do not match!!!

\n”;
}
[/php]

The password requirements can be easily modified. HTH John

P.S. Can anyone guess what the phrase of the password “Icsan2016!” is? (Hint: Look at the time I posted this) :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service