Jump to content

Archived

This topic is now archived and is closed to further replies.

crimsonmoon

Way to stop all the ASCII Characters?

Recommended Posts

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!

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Letters and numbers [i]are[/i] ASCII. Those are "extended ASCII" or "high ASCII."

Share this post


Link to post
Share on other sites
Obsidian,

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

Rich

Share this post


Link to post
Share on other sites
In Obsidians post what are the |^ and +$| characters for?  I understand the i is for case insensitive.

Share this post


Link to post
Share on other sites
The [color=red]|[/color] are the delimeters
the [color=red]^[/color] matches start of line
the [color=red]+[/color] matches one or more of the previous characters/classes (in this case classes)
the [color=red]$[/color] matches end of line

Rich

Share this post


Link to post
Share on other sites
A note that's very important when using regular expressions is the following quantifiers:

[color=red]*[/color] means 'Zero or more'
[color=red]?[/color] means 'Zero or one'
[color=red]+[/color] means 'One or more'

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

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

Note the * instead of +

Then an empty string would be considered as valid.

Rich

Share this post


Link to post
Share on other sites
[quote author=HuggieBear link=topic=105611.msg421957#msg421957 date=1156518884]
Obsidian,

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

Rich
[/quote]

thanks, rich... signature tends to get messed up on SMF for some reason.

Share this post


Link to post
Share on other sites
Cheers, so if I do :
[code]
<?php

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

?>
[/code]

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

Share this post


Link to post
Share on other sites
Correct, but you won't get the rest of the string, you realise that right?

Richard will yield RIC not RIChard.

Regards
Rich

Share this post


Link to post
Share on other sites
[quote author=scottybwoy link=topic=105611.msg421983#msg421983 date=1156522306]
Cheers, so if I do :
[code]
<?php

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

?>
[/code]

I will get the first three letters entered in the string capitalised. Yeah?
[/quote]

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

?>
[/code]

Share this post


Link to post
Share on other sites

×

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.