PDA

View Full Version : Disallow Space in Username


d1jsp
01-22-2014, 06:06 PM
Hello folks,

I have googled this already before coming here, and my research has not been successful. I know how to accomplish this through modifying the code, but I want to minimize modifying vbulletin standard code to keep the files scalable with updates.

I use the Username Regular Expression :
^[A-Z0-9]

Which the help function suggests disallows spaces. However, users are still popping up with spaces in their usernames.

noppid
01-22-2014, 06:13 PM
Use a different test. http://php.net/manual/en/function.strpos.php

kh99
01-22-2014, 06:19 PM
I use the Username Regular Expression :
^[A-Z0-9]

I think the expression should be
^[A-Z0-9]+$

If you leave off the +$ then it's only saying that the name has to start with a letter or number. But you should be aware that if you use that expression, then names can only contain letters or numbers (and no other symbols), so you would need to add any other symbols you want to allow.

Of course, none of this will affect any existing names.

d1jsp
01-22-2014, 06:28 PM
I think the expression should be
^[A-Z0-9]+$

If you leave off the +$ then it's only saying that the name has to start with a letter or number. But if you use the expression I posted, then names can only contain letters or numbers (and no other symbols). Of course, none of this will affect any existing names.

This may be the problem; I will let you know.

I appreciate the response.

noppid
01-22-2014, 06:36 PM
You both forgot lower case letters and a few other characters. Just saying.

kh99
01-22-2014, 06:47 PM
You both forgot lower case letters and a few other characters. Just saying.

The code where that option is used looks like this:
if (!preg_match('#' . str_replace('#', '\#', $this->registry->options['usernameregex']) . '#siU', $username))
{
$this->error('usernametaken', htmlspecialchars_uni($username), $this->registry->session->vars['sessionurl']);
return false;
}



The 'i' in the siU at the end means ignore case, so that expression will allow lower case. I did mention that no other characters would be allowed, but I added to that post in case it wasn't clear what I meant.

JαρŘα?σoζ
01-30-2014, 04:55 AM
So if I put this code "^[A-Z0-9]+$" to Username Regular Expression in vBulletin Options, that should be worked? So user cannnot make username that has space in it. Am I right?

tbworld
01-30-2014, 05:35 AM
The 'i' in the siU at the end means ignore case, so that expression will allow lower case.

I guess I can shorten my regex then. :)

Thanks for pointing out that code @Kh99. I had no idea they appended to the regular expression. I learn at least 10 things every day around here.

kh99
01-30-2014, 09:19 AM
So if I put this code "^[A-Z0-9]+$" to Username Regular Expression in vBulletin Options, that should be worked? So user cannnot make username that has space in it. Am I right?

Yes. A user should then only be able to create a user name with letters and numbers only.