Author Topic: Detecting illegal characters - need help  (Read 1078 times)

0 Members and 1 Guest are viewing this topic.

Offline CorangarTopic starter

  • Irregular
  • Posts: 3
    • View Profile
Detecting illegal characters - need help
« on: March 08, 2007, 09:59:42 AM »
Here is the deal :)
i searched this forum and found a similar topic.
But the script didn't work for me and i dont wanna weak up the ghosts :D

Code: [Select]
if (!preg_match("/[a-z0-9]/i", $username){
   print "Username field has invalid characters!";
   exit;
}

Thats a code from "HuggieBear"
The thing is, script works for me in a way that it detects invalid characters but ONLY if all characters are invalid.
If i would insert as username "##$$" it would print "Username field has invalid characters!"
But if i insert "##C2" it would consider it as a valid character cuz of C and 2

With other words, i am searching for a script that would detect if it has ANY invalid character anywhere, no matter if valid characters were inserted.

Any help would be nice :)
thanks :)

Offline obsidian

  • Managed Insanity
  • Staff Alumni
  • Freak!
  • *
  • Posts: 6,440
  • Gender: Male
  • Talk to me, I won't bite... hard.
    • View Profile
    • Guahan Web
Re: Detecting illegal characters - need help
« Reply #1 on: March 08, 2007, 10:04:20 AM »
A slight alteration to the regexp will solve that. You need to tell the regexp engine to require all the characters to be within your ranges provided:
Code: [Select]
<?php
if (!preg_match("/^[a-z0-9]+\z/i"$username)) {
  
// Your username contains invalid characters
}
?>


Or, you could simply match for any occurrence of a character not within your range (this may be better):
Code: [Select]
<?php
if (preg_match('|[^a-z0-9]|i'$username)) {
  
// Your username contains invalid characters
}
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

Code: [Select]
<?php
while (count($life->getQuestions()) > 0)
{   
$life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

Offline rantsh

  • Enthusiast
  • Posts: 115
    • View Profile
    • Tecnologia para todos
Re: Detecting illegal characters - need help
« Reply #2 on: March 08, 2007, 10:09:12 AM »
Do you need to use preg? I guess ereg has is an easier function

I guess you could use something like

Code: [Select]
if(ereg("[a-z]|[A-Z]|[0-9]";$var)) {
  //valid
} else {
  //invalid
}

That'll look for any alpha-num char and return true if present.

Hope I was helpful, and btw, I guess this topic goes on the Regex forum...

Regards,

Roderick
Roderick Smith.
Vivophone.com.
Get a free VIVOphone account with 30 prepaid minutes to call anywhere in the US (or some minutes to other places) by going to http://www.vivophone.com/vivosoft/index.php?promo=SR0979

Offline effigy

  • Staff Alumni
  • Freak!
  • *
  • Posts: 7,301
  • Gender: Male
  • We must be the change we wish to see in the world.
    • View Profile
Re: Detecting illegal characters - need help
« Reply #3 on: March 08, 2007, 10:14:51 AM »
Do you need to use preg?...I guess you could use something like ...[a-z]|[A-Z]|[0-9]...

Per the docs:
Quote
Note:  preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg().

Also, never alternate like that--combine the class: [A-Za-z0-9]
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

Offline CorangarTopic starter

  • Irregular
  • Posts: 3
    • View Profile
Re: Detecting illegal characters - need help
« Reply #4 on: March 08, 2007, 01:21:12 PM »
Code: [Select]
<?php
if (!preg_match("/^[a-z0-9]+\z/i"$username)) {
  
// Your username contains invalid characters
}
?>

This works perfectly, thank you all :P