How to format php preg_match() regular expression pattern

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 :-

<?php
//At least 1 upper, 1 lower, 1 number, 0 special, 0 spaces 8-15 chars alphanumeric only
$string = 'AAA1111a@';,
$pattern = '/^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?!.* ).{8,15}$/';
if(preg_match($pattern, $string)){
   echo "String 1 PASSES<br>";
} else {
   echo "String 1 FAILS<br>";
}

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:
    }

It is certainly helpful thanks

based on the above…
"/^(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])[a-zA-Z0-9]{8,15}$/"

Many thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.