Solved

PHP Password RegEx

Posted on 2006-11-25
8
508 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:
John Kawakami 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:evco
ID: 18014298
Is it impossible to do the length check within the regex? It would be preferrable that way.
0
 
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:John Kawakami
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

726 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