Jump to content

Regular Expression Help!


wasimkham

Recommended Posts

[b]$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>";
}[/b]

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 [b]correct[/b]

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

Thanks
Link to comment
Share on other sites

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

[code]$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>";
}[/code]

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.