Jump to content


Photo

Need Help Badly In Creating A Ereg Pattern


  • Please log in to reply
9 replies to this topic

#1 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 28 June 2006 - 10:48 PM

Hi, I need your help badly I am new to php and regular expression,, so please bear with me.

I need to know how do i create an eregi pattern that will ensure the following password match.

Password may start with a number, lower or upper case letter, but must include at least one number.

I tried this pattern

^[a-zA-Z0-9]{0,7}[0-9]*[a-zA-Z0-9]*$

But it is clearly weak as it will validate abcdefg5hi but not 1234567T890

Please help this is driving me nuts.

#2 Eternally777

Eternally777
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 29 June 2006 - 01:03 AM

I'm new at this stuff to, but I in one of my scripts I use the preg_match() function in an if statement to search for a certain pattern of characters in some of my strings, so maybe it will work for you too. I'm still pretty bad with the expressions, character ranges, and quantifiers though. Sorry.

#3 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 29 June 2006 - 10:05 AM

its not weak at all!

that expression is doing exactly as it is told!!! ;)

what you have said in the regex is

match any string where first 7 chars are alphanumeric follwoed by any numer of numbers follwed by anynumber of alphanumeric characters.

the first string you have is abcdefg5hi
abcdefg - 7 alphanumeric
5 - any number of numers
hi - any number of alphanumeric.

your second string is 1234567T890
1234567 - 7 alphanumeric
T - FAILS HERE AS THIS IS NOT A NUMBER!
90 - any number of numbers

those are two completely different patterns that you are trying to match.

come back with a explanation of what you're trying to achieve......
follow me on twitter @PHPsycho

#4 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 30 June 2006 - 06:34 PM

This regex will match a string with at least one number, being at least 7 characters long containing numbers or  letters. ie the string can contain only numbers.

If you'd like something else then you'll need to provide a better explanation as ToonMariner pointed out.
<?php
$strings = array();
$strings[] = '9Akkdlslsoei';
$strings[] = 'uhdiuehwdie';
$strings[] = 'dhs9';
$strings[] = '1234567T890';
$strings[] = 'abcdefg5hi';

foreach($strings as $string)
{
    if (!preg_match('/^(?=.*?[0-9])[a-zA-Z0-9]{7,}$/', $string))
    {
        print $string." is invalid <br />\n";
    }
    else
    {
        print $string." is valid <br />\n";
    }
}
?>


#5 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 04 July 2006 - 07:23 PM

Basically, what i want to do is to ensure that when registering users use strong passwords, ie at least one number, uppercase letter and symbol.  What i need to know therefore is how do i go about accomplishing this.

#6 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 04 July 2006 - 09:13 PM

Ok i read some more tutorials on regular expressions and came up with this.

^(?=.*?[0-9])(?=.*?[A-Z])(?=.*?[\W])[a-zA-Z0-9\W]{7,}$

It's close to the final expression that i need but its weak in 1 key area. The 7-digit sequence may start with a number, which i dont want it to do.

If you guys have any ideas please let me know. Also if u have any tips on refining or optimizing the expression please let me know also.

Thanks in advance for your help.



#7 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 04 July 2006 - 09:26 PM

Alright i think I have got it. Please see the final expression

^[a-zA-Z]?(?=.*?[\d])(?=.*?[A-Z])(?=.*?[\W])[\w\W]{8,15}$

I ran i through the Regex Coach and it passed. However, if you guys see any weaknesses or know of a better way of achieving the same results please let me know.

Thanks


#8 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 05 July 2006 - 12:45 PM

^[a-zA-Z]?(?=.*?[\d])(?=.*?[A-Z])(?=.*?[\W])[\w\W]{8,15}$

With the pattern you posted, you're still allowed to have a number at the beginning because "[a-zA-Z]?" means that the [a-zA-Z] is optional.

If you remove the "?" at the end it'll be close to what you want, but because you have the lookaheads comming after the "[a-z][A-Z]" match at the beginning of the string the pattern won't match a string starting with and containing only one capital letter.

eg:
Aks#9sd9w

I think the following is closer to what you want
/^(?=.*?[\d])(?=.*?[A-Z])(?=.*?[\W])[a-zA-Z].{8,15}$/s
Note that [\w\W] is effectively saying "Any Character" which is what "." is generally used for. Although the additional "s" modifier is needed to have it match newline characters. You can change it if you'd like.

EDIT: I should also mention that the password's length is currently required to be at least 9 characters long. You'll want to change the "8" in "{8,15}" to "6" if 7 is what you're going for.

#9 Wildbug

Wildbug
  • Members
  • PipPipPip
  • Advanced Member
  • 1,149 posts

Posted 05 July 2006 - 01:55 PM

You could have done this with multiple if statements with far less complexity and more readability than a single regular expression.
if (
    strlen($password) <= 15
    && strlen($password) >= 8
    && preg_match('/[A-Z]/',$password)
    && preg_match('/[a-z]/',$password)
    && preg_match('/^[^0-9]/',$password)
    && preg_match('/\d/',$password)
    && preg_match('/\D|\W/',$password)
) { // ....

In addition, you could split up those conditions into multiple if statements, allowing your program to inform the user of a specific error, i.e., "Password too short" or "Password may not start with a number."  Not to mention, this will make more sense when you come back to it in a few months....  :)
Twice a day my clock works PERFECTLY!  I can't figure out what's wrong with it.

#10 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 05 July 2006 - 09:22 PM

Thank you guys for all your help in answering all my questionsand for your patience.

Also Special thanks to Karma: +0/-0





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users