Jump to content


Photo

Regular Expression Help!


  • Please log in to reply
3 replies to this topic

#1 wasimkham

wasimkham
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 02 May 2006 - 01:18 PM

$pass_reg = ereg("^[0-9a-zA-Z]{5,12}",'1124adadsadasdasdsad',$match);
print_r($match);

if ($pass_reg) {
echo "<p>Password is <b>correct</b><p>";
} else {
echo "<p>Password is <b>incorrect</b><p>";
}


Hi, im using this regular expression "^[0-9a-zA-Z]{5,12}" as password validation, but even if the string passed through ereg() is more than 12 characters, the ereg() function still returns True, and $match returns the first 12 characters of the string. This is the output of the script:

Array ( [0] => 1124adadsada )

Password is correct

Can i configure it so that i can make it return False if the string passed is 12 characters or less?

Thanks

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 02 May 2006 - 01:33 PM

I dont know much about regex, but you can just do this:

$pass="1124adadsadasdasdsad";
$pass_reg = ereg("^[0-9a-zA-Z]",$pass,$match);

if ($pass_reg && strlen($pass)<=12) {
echo "<p>Password is <b>correct</b><p>";
} else {
echo "<p>Password is <b>incorrect</b><p>";
}

Btw, look at this (quoted from php.net, the ereg function):
"Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg(). "

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 wasimkham

wasimkham
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 02 May 2006 - 02:05 PM

well im gonna try perl regular expressions, i've just started learning them. but the thing wrong with your method is that ereg() function doesnt return a string, it returns a Boolean. thanks for you help, be back later if any problems

#4 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 02 May 2006 - 02:11 PM

I know it retuns a boolean... The piece of code I wrote does check if its alpha numeric and if its length is smaller than 12 (Forgot to add the: " && strlen($pass)>=5" to the if).

Orio.
Think you're smarty?

(Gone until 20 to November)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users