Jump to content


Photo

Way to stop all the ASCII Characters?


  • Please log in to reply
11 replies to this topic

#1 crimsonmoon

crimsonmoon
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 25 August 2006 - 03:04 PM

Is there a way to only allow number and letter in a field and not allow all those ASCII characters like ‡¤ñ etc etc


I don't like them for usernames as sometimes the browser doesn't read them and they make searching a pain!

#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 August 2006 - 03:08 PM

In Perl I'd use a regex to check that the field only contains Alpha-Numeric characters.  I'm sure php is capable of the same, but it may have a better function for it.

I'll look into this now.

Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 25 August 2006 - 03:09 PM

have you tried running a preg_match() on the user names?
<?php
if (!preg_match('|^[a-z0-9]+$|i', $_POST['username'])) {
  // contains invalid characters
  // only allows letters and numbers
}
?>

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

<?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

#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 25 August 2006 - 03:12 PM

Letters and numbers are ASCII. Those are "extended ASCII" or "high ASCII."
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 August 2006 - 03:14 PM

Obsidian,

You need to remove the additional 'http://' in the 'RegEx' link in your signature.  Just tried to get there  :)

Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 25 August 2006 - 03:22 PM

In Obsidians post what are the |^ and +$| characters for?  I understand the i is for case insensitive.

#7 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 August 2006 - 03:30 PM

The | are the delimeters
the ^ matches start of line
the + matches one or more of the previous characters/classes (in this case classes)
the $ matches end of line

Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#8 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 August 2006 - 03:42 PM

A note that's very important when using regular expressions is the following quantifiers:

* means 'Zero or more'
? means 'Zero or one'
+ means 'One or more'

And the reason that Obsidian has added the + at the end of the character class [a-z0-9] is to indicate that there must be some data entered, had he have put the following:

|^[a-z0-9]*$|i

Note the * instead of +

Then an empty string would be considered as valid.

Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#9 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 25 August 2006 - 03:52 PM

Obsidian,

You need to remove the additional 'http://' in the 'RegEx' link in your signature.  Just tried to get there  :)

Rich


thanks, rich... signature tends to get messed up on SMF for some reason.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?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

#10 scottybwoy

scottybwoy
  • Members
  • PipPipPip
  • Advanced Member
  • 532 posts
  • LocationUK

Posted 25 August 2006 - 04:11 PM

Cheers, so if I do :
<?php

$customer = preg_match('|^[a-z]+$i', $_POST['CUSTOMER']);
$customer = strtoupper($customer);
$cust = substr($customer, 0, 2);

?>

I will get the first three letters entered in the string capitalised. Yeah?

#11 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 August 2006 - 04:14 PM

Correct, but you won't get the rest of the string, you realise that right?

Richard will yield RIC not RIChard.

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#12 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 25 August 2006 - 05:00 PM

Cheers, so if I do :

<?php

$customer = preg_match('|^[a-z]+$i', $_POST['CUSTOMER']);
$customer = strtoupper($customer);
$cust = substr($customer, 0, 2);

?>

I will get the first three letters entered in the string capitalised. Yeah?


well, with that, you're not actually doing anything with the preg_match(). also, you're missing your closing delimiter. you'll need to do something like this:
<?php
if (preg_match('|^[a-z]+$|i', $_POST['CUSTOMER']);
  // valid match
  $customer = strtoupper($_POST['CUSTOMER']);
  $cust = substr($customer, 0, 2);
}

?>

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

<?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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users