Solved

PHP Password RegEx

Posted on 2006-11-25
8
504 Views
Last Modified: 2010-05-18
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!
0
Comment
Question by:evco
8 Comments
 
LVL 1

Assisted Solution

by:ictinus
ictinus earned 25 total points
ID: 18013668


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
 
LVL 8

Accepted Solution

by:
jk2001 earned 50 total points
ID: 18013731
You have to use two tests.

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

and

strlen() test for length

0
 
LVL 29

Assisted Solution

by:TeRReF
TeRReF earned 25 total points
ID: 18013873
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
 

Author Comment

by:evco
ID: 18014298
Is it impossible to do the length check within the regex? It would be preferrable that way.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 16

Assisted Solution

by:HackneyCab
HackneyCab earned 25 total points
ID: 18014708
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
 

Author Comment

by:evco
ID: 18014916
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
 
LVL 8

Expert Comment

by:jk2001
ID: 18015650
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
 

Author Comment

by:evco
ID: 18019621
:) Thanks, jk2001
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now