PDA

View Full Version : Disable certain characters in usernames


Abronsyth
12-16-2015, 06:36 PM
Resolved

Hello!

So despite the very obvious warning I put on my registration page I still have users registering with special characters in their usernames, which makes it so that their profiles become unviewable and makes for some issues.

Is there a way to disable it so that if they attempt to register with a special character in the username it won't register them, but instead gives the warning "Sorry, please only use letters, numbers, or spaces in your username."

Abronsyth
02-18-2016, 12:35 PM
OK, so there's this chunk of code;
if($username == "SYSTEM"){
$this->seterror("Cannot use SYSTEM as username.");
return FALSE;
}

But I'm wondering if I could do something like this to make it so the only characters that can be entered are alphanumeric and spaces? So if a user tries something like; "user_name" then they'll get the error; "Sorry, you cannot use special characters in your username."

Some folks use this sort of code;
if(preg_match('/^[a-zA-Z0-9 ]+$/', $username)) {
}

Do y'all think I'd be able to incorporate that somehow..?

pachoofoosh
02-18-2016, 05:36 PM
To check that usernames only have letters, numbers, and spaces, find the usernamevalidate() function in classes/class_registervalidator.php and replace the function with this:

protected function usernamevalidate($username = ""){
// The username validator, note its a bit complicate here due to the different mechanism of username check for register and other validators
$lang = Registry::get("lang");
if(!$this->emptyvalidate($username)){
$this->seterror("The field Username is Empty.");
return FALSE;
}
if(preg_match('#^[A-Z0-9 ]+$#i', $username) == FALSE){
$this->seterror("Usernames can only contain letters, numbers, and spaces.");
return FALSE;
}
$username = (empty($username))?$this->value['username']:$username;
$userexist = $this->datavalidate("users", array("username"), "username = '{$username}'");
if($userexist == TRUE){
$this->seterror($lang->user);
return FALSE;
}
else return TRUE;
}

Abronsyth
02-18-2016, 07:38 PM
Awesome, thank you!

Also- Pachooooo! <3