开发者

check for special characters in user name and numbers and special characters in first name, and last name form entry

I have a simple registration script I'm practicing with and I was wondering how I could check for special characters and numbers. Basically, for the user name area, no special 开发者_如何学Pythoncharacters are allowed. For the first name , last name area, no special characters and numbers are allowed. Would this be a regex operation?


When I post information to php from a form I like to use the ctype functions, its what they are for. http://php.net/manual/en/book.ctype.php

So if you wanted to a-zA-Z you could

if( !ctype_alpha( $str ) )
   die( 'Invalid characters' );

Or if you wanted a-zA-Z0-9 you could

if( !ctype_alnum( $str ) )
   die( 'Invalid characters' );


$stringWithout = preg_replace('/[^a-zA-Z]/', '', $string);
$stringWith = $string;
if ($stringWith == $stringWithout) {
    //string is clean
}

Something like this perhaps? If you replace every character but a-z and A-Z with nothing, and compare them, then if they are the same, you will know what they typed in has to be only characters like a-z and A-Z.

And yes, this uses a regex to replace the characters.


Don't try to reject stuff you don't want - you'll always find out later that users are much more imaginative than you.

Only accept what you know you can handle. If you only want latin letters and spaces, validate that that is the only thing you're getting with something like:

/^[0-9a-zA-Z ]+$/

Add only the characters you trust in there if I missed some.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜