Setting criteria for user passwords

I have the following lines of code that forces a minimum length of the password, but I need to enforce a stricter password policy where there has to be both letters and numbers in each password. I need to rewrite this snippet:

else if (strlen($_POST['password']) < 5) {

	$this->UserInterface->Error(true, 'Please choose password greater than 5');


	return false;

Open in new window

Evert JorDVM/ResearcherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
There's some code on this page that looks good:   It checks both size and characters.
Julian HansenCommented:
CPU's are fast so calling preg_match a million times has negligible impact however I am an efficiency nut - here is the code I use - it is quite versatile in terms of changing to meet requirements
define('MIN_LENGTH', 6);
define('MAX_LENGTH', 20);

$password = "PASs!2RDDD";

if (validatePassword($password) == 0x0f) {
  echo "Password is fine<br/>";
else {
  echo "Password is invalid<br/>";

function validatePassword($password, $min_length = MIN_LENGTH, $max_length=MAX_LENGTH) 
  $len = strlen($password);
  $check = 0;
  if ($len >= $min_length && $len <= $max_length) {
    for($i=0; ($i < $len) && ($check != 0x0F); $i++) {
      if ($password[$i] >= 'a' && $password[$i] <= 'z') $check = $check | 0x01; // Lower Case
      if ($password[$i] >= 'A' && $password[$i] <= 'Z') $check = $check | 0x02; // Upper Case
      if ($password[$i] >= '0' && $password[$i] <= '9') $check = $check | 0x04; // Numeric
      if (($password[$i] >= '!' && $password[$i] <= '/') ||
            ($password[$i] >= ':' && $password[$i] <= '@') ||
            ($password[$i] >= '[' && $password[$i] <= '`') ||
            ($password[$i] >= '{' && $password[$i] <= '~')) $check = $check | 0x08; // Alt chars
  return $check; // Returns 0 if string is not the right length

Open in new window

The nice thing about this code is that the return value tells you what is missing from the password.

If bit 0 is not set you are missing a lower case char
If bit 1 is not set you are missing an upper case char
To find out what you are missing simply XOR with 0x0F which gives you a number that can be translated back into a condition.
Julian HansenCommented:
Hmm - just ran some performance tests against posting above and one using preg_match - preg_match won hands down.
So alternative using preg_match
function validatePassword2($password, $min_length = MIN_LENGTH, $max_length=MAX_LENGTH) 
  $len = strlen($password);
  if ($len >= $min_length && $len <= $max_length) {
    if( !preg_match("#[0-9]+#", $password) ) return -1;
    if( !preg_match("#[a-z]+#", $password) ) return -2;
    if( !preg_match("#[A-Z]+#", $password) ) return -3;
    if( !preg_match("#\p{P}|\p{S}+#", $password) ) return -4;
  else {
  return -99; // String too long or too short
  return 0;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
All that password variant stuff is utter nonsense.  The plain fact is that you can make life "password hell" for your clients by making up capricious rules, but you can't outrun the cryptologists (good or bad) that seek to uncover and crack your passwords.  A better strategy might be a "passphrase" with a minimum word length.  If you want to see how unsuccessful you are likely to be in controlling and concealing passwords, have a look at this article.  It's rather old news by now.

I wish I had a good solution to offer, but with current technology and the minimalist password rules like "one upper, one lower, one punctuation, one number" you're out on thin ice, and if what you're trying to protect has any value you need to think about much more than just a password.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.