PHP Password RegEx


I'm trying to produce a regex for a password with the following requirements:
Min. 8 characers with at least two digits/numbers

Two or more numbers anywhere in a does that translate to a regex?

(?=.*/d) is at least one number, but I cannot figure out how to do at least two numbers.

Who is Participating?
John KawakamiConnect With a Mentor Commented:
You have to use two tests.

/.*\d.*\d.*/  -- tests that there are two digits


strlen() test for length

ictinusConnect With a Mentor Commented:

this may or may not work honestly I didn't take time to learn the program but it proclaims itselft to be a regex builder.  hopefully it's usefull to you.  
TeRReFConnect With a Mentor Commented:
How about:


function checkPasswd($pw) {
        if ((preg_match('/^.*\d+.*\d+.*?$/', $pw, $match)) === 1)
                if (strlen($match[0]) > 7)
                        return true;
        return false;

$s = 'pass2word6';
if (checkPasswd($s) === true)
        echo 'Good Password';
        echo 'Bad Password';

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

evcoAuthor Commented:
Is it impossible to do the length check within the regex? It would be preferrable that way.
HackneyCabConnect With a Mentor Commented:
As far as I know, it's not possible to enforce a length using regex where you have more than one character class, so you are best off using the strlen() function in PHP.

Is there a reason why you don't want to use strlen()?
evcoAuthor Commented:
I'm using the PEAR QuickForm class and passing a regex to validate the password field. Obviously, I can work around that and use the strlen() function.

Thanks all, for your help.
John KawakamiCommented:
If you can pass two regexs to the validation thing, you can also do this:

That will specify that there are 8 chars or more.
evcoAuthor Commented:
:) Thanks, jk2001
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.