PHP Password RegEx

Hi,

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 string...how does that translate to a regex?

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

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

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

and

strlen() test for length

0
 
ictinusConnect With a Mentor Commented:


http://www.ultrapico.com/ExpressoDownload.htm

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.  
0
 
TeRReFConnect With a Mentor Commented:
How about:

<?php

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';
else
        echo 'Bad Password';

?>
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
evcoAuthor Commented:
Is it impossible to do the length check within the regex? It would be preferrable that way.
0
 
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()?
0
 
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.
0
 
John KawakamiCommented:
If you can pass two regexs to the validation thing, you can also do this:
/.{8,}/

That will specify that there are 8 chars or more.
0
 
evcoAuthor Commented:
:) Thanks, jk2001
0
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.