validate help

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

validate help

Post by speedy33417 »

I know it's not a fair request, but I was hoping somebody could help me out with this.

I'm trying to validate two user inputs.

The first ($foo) should only pass if characters used are: a-z 0-9 and -_ (dash and underscore).

The second($bar) only if characters are: a-z A-Z 0-9 -_.,?!&()

This whole ereg thing is a little way too new to me. I've spent about 4 hours on this and I'd rather not post the code I have right now. People would be laughing at it (and me) for years to come...
Puh-please!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What have you tried?
User avatar
Cameri
Forum Commoner
Posts: 87
Joined: Tue Apr 12, 2005 4:12 pm
Location: Santo Domingo, Dominican Republic

Post by Cameri »

Code: Select all

/*The first ($foo) should only pass if characters used are: a-z 0-9 and -_ (dash and underscore).

The second($bar) only if characters are: a-z A-Z 0-9 -_.,?!&() 
*/

// Check if $foo is valid
if (preg_match('![a-z0-9\-_]+!',$foo)) {
 //passed
} else {
// failed
}

// Check if $bar is valid
if (preg_match('![a-z0-9\-_\.,\?!&\(\)]+!i',$bar)) {
 //passed
} else {
// failed
}
Suggestions, read about regular expressions, specially PERL regular expressions, on PHP they are called Perl-Compatible Regular Expressions (PCRE). Google is your best friend.
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

Thank you for your help Cameri. I tried your code, but I'm getting a strange result.

I think it only checks the first letter, because asd, as@#$ both pass, but @#$ does not.

Here's the code:

Code: Select all

if (preg_match('![a-z0-9\-_]+!',$albumName)) { 
   //passed 
   $message = "passed";
} else { 
   // failed 
   $message = "did not pass";	
}
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Cameri's code is close, but incorrect. Use:

Code: Select all

preg_match('!^[a-z0-9\-_]+$!i',$albumName)
and

Code: Select all

preg_match('!^[a-z0-9\-_.,?!&()]+$!i',$bar)
The ^ and $ ensure that the pattern matches the whole string, as running the original regex on "as@#$" matches the "as" and thus returns true, incorrect behavior.

Really, you should check out the tutorial on this forum and get a general idea on how to use regex. They're really powerful and worth learning.
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

Quick tip. Ambush Commander already removed the backslashes before the chars within the character class. Note that you don't have to escape the dash neither when you put it right after the opening square bracket of the character class.

Code: Select all

preg_match('!^[-a-z0-9_.,?!&()]+$!i',$bar)
Post Reply