MyBB Community Forums

Full Version: Prevent users from registering usernames that start with a number?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Any idea how I can set this up? I've already coded my plugin to check if the first character of the username is a number and if so to replace it with a random letter but after thinking about this more I've realized that this can cause issues down the road if somebody actually registers that username (like the plugin will change 1user to auser but it doesn't rename the user's MyBB account so if somebody creates a user named auser when they go to use the plugin they will get errors about conflicting usernames).

Can this be done with a hook possibly? I want to avoid changing the core code at all costs.
Why not just prevent registering of a user name with a starting number all together?
That's what this thread is about. Wink

So is there a way to do it without core file edits like I asked?
You can hook into the validation of the user:
$plugins->add_hook('datahandler_user_validate','YOURPLUGIN_validate_user');

function YOURPLUGIN_validate_user($datahandler)
{
	if(preg_match("/^[0-9]/",$datahandler->data['username']))
	{
		$datahandler->set_error('bad_characters_username');
		
		return false;
	}
	
	return true;
}

Tested and confirmed to work Wink
Thanks! Big Grin
Thanks again, it got me in the right direction. The code posted wouldn't let users use any numbers in their usernames at all so this is the code I used:

$plugins->add_hook('datahandler_user_validate','YOURPLUGIN_validate_user');

function YOURPLUGIN_validate_user($datahandler)
{
	$usrfc = substr($datahandler->data['username'], 0, 1);	
	if (is_numeric($usrfc))
    {
        $datahandler->set_error('bad_characters_username');
        
        return false;
    }
    
    return true;
}
May I ask, just where and how do you put this code into. I also would like to use this.
(2011-01-19, 01:55 AM)Dudley Wrote: [ -> ]May I ask, just where and how do you put this code into. I also would like to use this.
Hes creating a plugin. Are you going to release this publicly Kujoe
Yes. This plugin is in testing now.