FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP Preg_match() Help





moejoe
Hi,

Pretty stupid question, but i can't seem to google it!

I need a Preg_match string that will disable invalid characters for a password feild.

Disable Characters such as: ()\/`~' and it must not allow Spaces etc..ect..

Thanks in advance!!
fromegame
Code:
<?php
if(preg_match("/[A-Za-z0-9\'\"]/", $_POST['password'])) {
echo "Password valid";
}
else{
echo "Password invalid";
}
?>


PS: Adolescent thingy now Smile
hexkid
Preg_match() does not disable anything; it matches a string against a regular expression.

So I guess you need a regular expression that matches strings with one or more invalid characters (or, what is the same thing, a regular expression that matches strings with only allowed characters --- hmmm, is the empty string valid?)

invalid characters
Code:
/[()\\\/`~' ]/

The above regular expression will match strings with one or more of the cracaters specified between the square brackets (notice that "\" and "/" have been escaped). If you want to write that regular expression inside a php script you need to escape it even more
Code:
### NOT TESTED, I'm not sure about the need to escape `
$rxdoublequote = "/[()\\\\\\/`~' ]/";
$rxsinglequote = '/[()\\\\\\/`~\' ]/';


valid characters
Code:
/^[a-z0-9_-]*$/i

The above regular expression will match strings with zero or more of the characters between the square brackets and no other characters anywhere. Notice the regular expression is case insensitive because of the last "i".
Code:
/^[a-z0-9_-]+$/i

With a "+" instead of the "*" the regular expression matches string with one or more of the characters.

fromegame wrote:
Code:
if(preg_match("/[A-Za-z0-9\'\"]/", $_POST['password']))

This matches any string that has one of the characters between the square brackets even if it has other characters; for example "(a)" is matched by your regular expression.
moejoe
Quote:
/^[a-z0-9_-]*$/i


Works like A Charm!
Thanks for the background info too!

THANK YOU! Laughing
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.