• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 1.4 How to disable numbers usernames

#1
Hello guys, can you please help me with username registration. I want disable registration users with only numbers in username like 12547951. If they have for example Dan45, Asterix77 it is OK, just clear numbers registration.
 

AndyB

Well-known member
#3
This is what I use:

^[a-zA-Z0-9]+$

This allows all alphabet characters and numbers, but it also allows only a number. How would I use what I have and disallow just a number?
 

Jake Bunce

XenForo moderator
Staff member
#9
Admin CP -> Home -> Options -> User Registration -> User Name Match Regular Expression

Alpha only:

Code:
^[a-zA-Z]+$
Alpha+space only:

Code:
^[a-zA-Z ]+$
Alphanumeric only:

Code:
^[a-zA-Z0-9]+$
Alphanumeric+space only:

Code:
^[a-zA-Z0-9 ]+$
Alphanumeric+space+underscore only:

Code:
^[a-zA-Z0-9 _]+$
Alphanumeric+space+underscore+dash only:

Code:
^[a-zA-Z0-9 _\-]+$
 
#10
Cheers for that, although I don't think those would do what we were looking for exactly.

I think the OP was looking to allow letters and numbers, but disallow if ONLY numbers.
 

Jake Bunce

XenForo moderator
Staff member
#11
I think the OP was looking to allow letters and numbers, but disallow if ONLY numbers.
Mike's regex should work for that, though it doesn't explicitly say what the character range is, only that one must not be a number.

This is more explicit... this is alphanumeric only with at least one alpha somewhere:

Code:
^[a-zA-Z0-9]*[a-zA-Z][a-zA-Z0-9]*$
Or you may want to allow spaces also:

Code:
^[a-zA-Z0-9 ]*[a-zA-Z][a-zA-Z0-9 ]*$
 

ŽivaAkcija

Well-known member
#14
Hi i use PHP 7.10 and mariadatabase i try all codes above but none of them not works any help?
Got big problems with users who use caps lock in nick names.
 

Jake Bunce

XenForo moderator
Staff member
#15
Hi i use PHP 7.10 and mariadatabase i try all codes above but none of them not works any help?
Got big problems with users who use caps lock in nick names.
Hmm. My mistake. I referred you to this thread. But now I see in the code that the username regex uses an "i" flag which makes it case-insensitive:

library/XenForo/DataWriter/User.php

Removing the i would allow the regex to be case-sensitive, but that requires a file edit or addon.

Code:
			$matchRegex = $this->getOption(self::OPTION_USERNAME_REGEX);
			if ($matchRegex)
			{
				$matchRegex = str_replace('#', '\\#', $matchRegex); // escape delim only
				if (!preg_match('#' . $matchRegex . '#i', $username))
				{
					$this->error(new XenForo_Phrase('please_enter_another_name_required_format'), 'username');
					return false;
				}
			}
 

Robert9

Active member
#17
[A-Z]{1,1}[a-z0-9\-\.]{3,14}

So this will still take "name" instead of "Name"?

Is there a hook around this register test?
Can i add some php code before?
I would like to delete spaces and everything that is not allowed, set all to lower case and a ucfirst, bevor the regex starts. :)
 

Robert9

Active member
#19
My changes in vb > register.php from 1912:

Code:
       // fix extra whitespace and invisible ascii stuff                                                                       
        $username = trim(preg_replace('#\s+#si', '', strip_blank_ascii($username, ' ')));                                       
                                                                                                                                              
        //// lower                                                                                                 
        $username = strtolower($username);                                                                                     
                                                                                                                                                  
        // Umlauts
        $array_1 = array("ä","ö","ü","ß");                                                                                     
        $array_2 = array("ae","oe","ue","ss");                                                                                 
        for($x=0;$x<4;$x++) {                                                                                                                       
           $username = str_replace($array_1[$x],$array_2[$x],$username);                                                       
       }
      
       // Only one!
       $username = str_replace("--","",$username);
       $username = str_replace("..","",$username);
       $username = str_replace("__","",$username);
      
       $username = str_replace("._","",$username);
       $username = str_replace(".-","",$username);
       $username = str_replace("-_","",$username);
       $username = str_replace("-.","",$username);
       $username = str_replace("_-","",$username);
       $username = str_replace("_.","",$username);       
      
        
       // Ucase first                                                                                                 
       $username = ucfirst($username);
For shure an addon would be nice, but it is not done in 2 minutes like changing the code,
what can i do.