MyBB Community Forums

Full Version: Space in username
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

Today when I checked my forum, I could see some members are using space in their usernames. I don't want my forum member's username with space. So how can I prevent users from entering a username with space or any special charecters. Please help me
<?php

/*
Plugin Alpha Username
(c) 2006 by democracyforums.com Labrocca
Website: http://www.mybbcentral.com
*/

function alpa_username_info()
{
	return array(
		"name"			=> "Alpha Usernames",
		"description"	=> "Prevents users registering without an alpha username.",
		"website"		=> "http://www.democracyforums.com",
		"author"		=> "labrocca",
		"authorsite"	=> "http://www.mybbcentral.com",
		"version"		=> "1.0",
	);
}

$plugins->add_hook("member_do_register_start", "alpha_username");


function alpha_username_activate()
{
}

function alpha_username_deactivate()
{
}

function alpha_username()
{
	global $mybb, $session, $db;
	if(!ctype_alpha($mybb->input['username']))
		{
			error("Sorry, you cannot register that username. It must be letters only with no spaces or numbers.");
		}
}

?>

There I made one for you. Upload it to your inc/plugins/ directory as alpha_username.php. Test it to make sure it works as you require.


 I am going to bed but if you can't figure it out then tomorrow I can help you.
Thanks labrocca. Thanks a lot
What if we want to allow numbers in username?
Then you can whether edit their username lalter through Admin CP or Add them trough admin CP or disable the mod let them register then enable it.

This plugin works upon registring, any existing member will not be affected, and ofcourse you dont need anything more than that.
 if(!ctype_alpha($mybb->input['username']))

That's the line with the nuts and bolts. the ctype_alpha basically is a php function to check if the characters are only alpha. The "if(!ctype_alpha) means "If characters are NOT alpha".

Now here are some other php functions that you can use to alter this plugin for your needs.

ctype_alnum — Check for alphanumeric character(s)
ctype_alpha — Check for alphabetic character(s)
ctype_cntrl — Check for control character(s)
ctype_digit — Check for numeric character(s)
ctype_graph — Check for any printable character(s) except space
ctype_lower — Check for lowercase character(s)
ctype_print — Check for printable character(s)
ctype_punct — Check for any printable character which is not whitespace or an alphanumeric character
ctype_space — Check for whitespace character(s)
ctype_upper — Check for uppercase character(s)
ctype_xdigit — Check for character(s) representing a hexadecimal digit

You can check them out in more detail at the php.net official site.