Welcome Guest, Not a member yet? Register   Sign In
Allow Spaces but not other random characters?
#1

[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.
#2

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

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

[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.
#5

[eluser]Buso[/eluser]
http://ellislab.com/codeigniter/user-gui...#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
#6

[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
#7

[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




Theme © iAndrew 2016 - Forum software by © MyBB