?
Solved

PHP Password RegEx

Posted on 2006-11-25
8
Medium Priority
?
511 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 100 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 200 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 100 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 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 …
Suggested Courses

777 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