Skip to content

Instantly share code, notes, and snippets.

@matula
Last active December 13, 2015 19:18
Show Gist options
  • Save matula/4961339 to your computer and use it in GitHub Desktop.
Save matula/4961339 to your computer and use it in GitHub Desktop.
Strong Password Validation. This was used by a gov't site that needed to use these rules.
<?php
/**
* Rule: strongPassword. Client requirements for a strong password
*
* @param string $field password
* @return boolean
*/
public function strongPassword($field) {
// Set RegEx values
$rule_uppercase = '/[A-Z]/';
$rule_lowercase = '/[a-z]/';
$rule_nonalpha = '/[(!@#$%^&*_+=?~`;:,<>|)0123456789]/';
// No non-alpha characters in first or last position
if (strpos($rule_nonalpha, substr($field, 0, 1)) !== FALSE or strpos($rule_nonalpha, substr($field, -1)) !== FALSE) {
return FALSE;
}
// At least one uppercase letter
if (preg_match_all($rule_uppercase, $field, $match) < 1) {
return FALSE;
}
// At least one lowercase letter
if (preg_match_all($rule_lowercase, $field, $match) < 1) {
return FALSE;
}
// At least 2 non-alpha characters
if (preg_match_all($rule_nonalpha, $field, $match) < 2) {
return FALSE;
}
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment