crimsonmoon Posted August 25, 2006 Share Posted August 25, 2006 Is there a way to only allow number and letter in a field and not allow all those ASCII characters like ‡¤ñ etc etcI don't like them for usernames as sometimes the browser doesn't read them and they make searching a pain! Quote Link to comment https://forums.phpfreaks.com/topic/18647-way-to-stop-all-the-ascii-characters/ Share on other sites More sharing options...
HuggieBear Posted August 25, 2006 Share Posted August 25, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/18647-way-to-stop-all-the-ascii-characters/#findComment-80361 Share on other sites More sharing options...
obsidian Posted August 25, 2006 Share Posted August 25, 2006 have you tried running a preg_match() on the user names?[code]<?phpif (!preg_match('|^[a-z0-9]+$|i', $_POST['username'])) { // contains invalid characters // only allows letters and numbers}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18647-way-to-stop-all-the-ascii-characters/#findComment-80362 Share on other sites More sharing options...
effigy Posted August 25, 2006 Share Posted August 25, 2006 Letters and numbers [i]are[/i] ASCII. Those are "extended ASCII" or "high ASCII." Quote Link to comment https://forums.phpfreaks.com/topic/18647-way-to-stop-all-the-ascii-characters/#findComment-80363 Share on other sites More sharing options...
HuggieBear Posted August 25, 2006 Share Posted August 25, 2006 Obsidian,You need to remove the additional 'http://' in the 'RegEx' link in your signature. Just tried to get there :)Rich Quote Link to comment https://forums.phpfreaks.com/topic/18647-way-to-stop-all-the-ascii-characters/#findComment-80365 Share on other sites More sharing options...
scottybwoy Posted August 25, 2006 Share Posted August 25, 2006 In Obsidians post what are the |^ and +$| characters for? I understand the i is for case insensitive. Quote Link to comment https://forums.phpfreaks.com/topic/18647-way-to-stop-all-the-ascii-characters/#findComment-80367 Share on other sites More sharing options...
HuggieBear Posted August 25, 2006 Share Posted August 25, 2006 The [color=red]|[/color] are the delimetersthe [color=red]^[/color] matches start of linethe [color=red]+[/color] matches one or more of the previous characters/classes (in this case classes)the [color=red]$[/color] matches end of lineRich Quote Link to comment https://forums.phpfreaks.com/topic/18647-way-to-stop-all-the-ascii-characters/#findComment-80370 Share on other sites More sharing options...
HuggieBear Posted August 25, 2006 Share Posted August 25, 2006 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]$|iNote the * instead of +Then an empty string would be considered as valid.Rich Quote Link to comment https://forums.phpfreaks.com/topic/18647-way-to-stop-all-the-ascii-characters/#findComment-80372 Share on other sites More sharing options...
obsidian Posted August 25, 2006 Share Posted August 25, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/18647-way-to-stop-all-the-ascii-characters/#findComment-80376 Share on other sites More sharing options...
scottybwoy Posted August 25, 2006 Share Posted August 25, 2006 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 Link to comment https://forums.phpfreaks.com/topic/18647-way-to-stop-all-the-ascii-characters/#findComment-80386 Share on other sites More sharing options...
HuggieBear Posted August 25, 2006 Share Posted August 25, 2006 Correct, but you won't get the rest of the string, you realise that right?Richard will yield RIC not RIChard.RegardsRich Quote Link to comment https://forums.phpfreaks.com/topic/18647-way-to-stop-all-the-ascii-characters/#findComment-80390 Share on other sites More sharing options...
obsidian Posted August 25, 2006 Share Posted August 25, 2006 [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]<?phpif (preg_match('|^[a-z]+$|i', $_POST['CUSTOMER']); // valid match $customer = strtoupper($_POST['CUSTOMER']); $cust = substr($customer, 0, 2);}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18647-way-to-stop-all-the-ascii-characters/#findComment-80397 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.