Disable certain characters in usernames

Forum
Last Post
Threads / Messages

Abronsyth

A Headache Embodied
Member
Joined
Aug 25, 2011
Messages
1,012
Points
36
Location
NY
Mysidian Dollar
73,285
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."
 
Last edited:
OK, so there's this chunk of code;
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;
PHP:
if(preg_match('/^[a-zA-Z0-9 ]+$/', $username)) {
}

Do y'all think I'd be able to incorporate that somehow..?
 
Last edited:
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:
PHP:
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;
  }
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,277
Messages
33,122
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Latest Posts

Top