Hi
I am trying to create a PHP preg_match() to validate a string to the following restrictions:-
Must contain 1 lower, 1 upper and 1 number
Must contain alphanumeric ONLY
Must NOT contain spaces or special characters
Must be 8 - 15 characters in length
I have managed to accomplish most of the above but I am unsure how to limit to alphanumeric only. My expression still accepts special characters or non-alphanumeric :-
While the following doesn’t give you exactly want you want. You probably can get what you want as it breaks it down pretty good in the comment section.
/*
*
* 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*$', $this->data['password'])) {
return \TRUE; // Valid Password:
} else {
return \FALSE; // Invalid Password:
}