CodeIgniter Forums
Allow Spaces but not other random characters? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Allow Spaces but not other random characters? (/showthread.php?tid=32512)



Allow Spaces but not other random characters? - El Forum - 07-26-2010

[eluser]Corey Freeman[/eluser]
For my blogging simulator (fun fun), people want to be able to use spaces in their blog's names. I personally want the game to retain some kind of realism by not letting people name their blogs random strings of special characters (e..x "U$%9*$)&%(#*$)*^)( Blog) but I don't see any reason not to allow spaces or something.

How do I keep the alphanumeric rule AND allow spaces? And I do know that a space isn't an alphanumeric character.


Allow Spaces but not other random characters? - El Forum - 07-26-2010

[eluser]mddd[/eluser]
Just make a custom rule, then you can allow or disallow everything you want.


Allow Spaces but not other random characters? - El Forum - 07-26-2010

[eluser]Corey Freeman[/eluser]
I don't know what that custom rule would look like.


Allow Spaces but not other random characters? - El Forum - 07-26-2010

[eluser]Jelmer[/eluser]
Something like this:
Code:
function check_blog_name( $str )
{
    return ( preg_match( '/[^a-z0-9 ]/i', $str ) === 0 );
}

EDIT: that matches anything but a to z, 0 to 9 or spaces (the "i" at the end makes it not care about upper/lowercase), if it finds more then 0 matches that means there are disallowed characters.


Allow Spaces but not other random characters? - El Forum - 07-26-2010

[eluser]Buso[/eluser]
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks

Use a callback to check for your desired regular expression.

More useful links:
http://php.net/manual/en/function.preg-match.php
http://www.regular-expressions.info/quickstart.html


Allow Spaces but not other random characters? - El Forum - 07-26-2010

[eluser]Corey Freeman[/eluser]
@buso
I know how to use a callback, I just meant I don't know how to check for an empty space.

@Jelmer
Thanks for the suggestion. Smile


Allow Spaces but not other random characters? - El Forum - 07-26-2010

[eluser]cchi[/eluser]
try this

regex: [^\s] -this check a whitespace characters or use [^\_]

\s - Match any whitespace characters (space, tab etc.)
\S - Match any character NOT whitespace (space, tab)

^[\S]*$ - check non whitespace string