oracle259 Posted June 28, 2006 Share Posted June 28, 2006 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 1234567T890Please help this is driving me nuts. Quote Link to comment Share on other sites More sharing options...
Eternally777 Posted June 29, 2006 Share Posted June 29, 2006 I'm new at this stuff to, but I in one of my scripts I use the [i]preg_match()[/i] 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. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted June 29, 2006 Share Posted June 29, 2006 its not weak at all!that expression is doing exactly as it is told!!! ;)what you have said in the regex ismatch 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 abcdefg5hiabcdefg - 7 alphanumeric5 - any number of numershi - any number of alphanumeric.your second string is 1234567T8901234567 - 7 alphanumericT - FAILS HERE AS THIS IS NOT A NUMBER!90 - any number of numbersthose are two completely different patterns that you are trying to match.come back with a explanation of what you're trying to achieve...... Quote Link to comment Share on other sites More sharing options...
shoz Posted June 30, 2006 Share Posted June 30, 2006 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. [code=php:0]<?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"; }}?>[/code] Quote Link to comment Share on other sites More sharing options...
oracle259 Posted July 4, 2006 Author Share Posted July 4, 2006 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. Quote Link to comment Share on other sites More sharing options...
oracle259 Posted July 4, 2006 Author Share Posted July 4, 2006 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. Quote Link to comment Share on other sites More sharing options...
oracle259 Posted July 4, 2006 Author Share Posted July 4, 2006 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 Quote Link to comment Share on other sites More sharing options...
shoz Posted July 5, 2006 Share Posted July 5, 2006 [quote=oracle259]^[a-zA-Z]?(?=.*?[\d])(?=.*?[A-Z])(?=.*?[\W])[\w\W]{8,15}$[/quote]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#9sd9wI think the following is closer to what you want[code]/^(?=.*?[\d])(?=.*?[A-Z])(?=.*?[\W])[a-zA-Z].{8,15}$/s[/code]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. Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 5, 2006 Share Posted July 5, 2006 You could have done this with multiple if statements with far less complexity and more readability than a single regular expression.[code]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)) { // ....[/code]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.... :) Quote Link to comment Share on other sites More sharing options...
oracle259 Posted July 5, 2006 Author Share Posted July 5, 2006 Thank you guys for all your help in answering all my questionsand for your patience. Also Special thanks to Karma: +0/-0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.