Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ereg Regular Expression problem 1

Status
Not open for further replies.

djhawthorn

Technical User
Mar 4, 2002
641
AU
I have the following regular expression

return ereg('^[a-zA-Z0-9]*([a-zA-Z]+[0-9]+)|([0-9]+[a-zA-Z]+)[a-zA-Z0-9]*$', $validate);

Which work for validating a character string containing only alphanumerics and ensuring the string contains at least 1 alpha and 1 numeric; but I need to ensure it is also at least 6 characters and no more than 10 characters in length.

I have tried various combinations like
(^[a-zA-Z0-9]*([a-zA-Z]+[0-9]+)|([0-9]+[a-zA-Z]+)[a-zA-Z0-9]*$){6,10} but this doesn't seem to work - is there an easy way to do this?

Cheers.

[ponder][laughtears] The dumber they think you are, the more surprised they'll be when you kill them! [machinegun][rofl2]
 
i can't help thinking that it's less hassle to do this in three steps. i doubt whether there would be a significant performance penalty since password complexity validation is a relatively 'rare' process in the life of an average application. and the amount of sweat and tears that people waste on tweaking regular expressions is not always justified.

Code:
function validateComplexity($password){
  if (!preg_match('/[a-z]?/i/', $password) ) $error[] = "you must include at least one alphabetical character in your password";
  if (!preg_match('/[0-9]?/', $password)) $error[] = "you must include at least one number in your password";
  if (strlen($password) < 6) $error[] = "your password must be at least six characters long";
  return (is_array($error)) ? $error : true;
}

if you _really_ want to do it in one regex then i think this will do it for you

Code:
function validatePasswordComplexity($password){
 return preg_match('/^(?=.*\d)(?=.*[A-Z]).{6,}$/i', $password);
 //returns true for a valid password, false for an invalid.
}

 
jpadie, I don't entirely disagree with your statement "and the amount of sweat and tears that people waste on tweaking regular expressions is not always justified." but there are some changes that can make a significant performance difference like using ungreedy REGEXs en lieu of greedy ones where possible and using negative character classes in lieu of ungreedy REGEXs where possible.
 
for sure. but not using regex's would be even quicker!
 
djhawthorn, try using a free program called REGEX Coach for debugging REGEXs.
 
I went down the path of strlen, and used a simple HTML form to debug.

Cheers for all the help.

[ponder][laughtears] The dumber they think you are, the more surprised they'll be when you kill them! [machinegun][rofl2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top