Forum Moderators: coopster
I have a members account in my site. When new members register they get to create their own user name. A sub folder is created with the same user name.
I would like to prevent new users from using special characters from their users name such as - / ) .! # and so on.
some one please show me a tutorial which explains how to use PHP functions to do this?
Any help will be appreciated.
Thank you,
Use ctype_alpha or ctype_alnum
So something like:
$username = $_POST['username'];
if(ctype_alpha($username))
{
//insert your code for success
}
else
{
//insert your code for a failed username
}
[us3.php.net...]
These are part of the character type checking functions available. . . read about them here:
[us.php.net...]
$username_pattern = 'a-z\d\-\_';
// or, following OP example, 'a-z\d'
// note that the following are IDENTICAL:
// /[A-Za-z0-9]/
// /[A-Z0-9]/i <-- case insensitive
// /[A-Z\d]/i
// /[\d\w]/ <-- May or may not work for characters;
// any digit, any word character, which may or may not
// include invalid characters depending on charset.
if (preg_match('/[^$username_pattern]/i', $string )) {
// re-output the form with warning
// ^ when first in a class [] means "not." So
// anything NOT these characters will fall here.
}
else {
// do the signup
}
A very good piece of advice that may seem small,
reject the attempt and request the user to do it again.
User thinks their user name was accepted, you FIX it, so when they log in, it will reject the log in because they don't know you altered it.