Permalink
Browse files

Merge pull request #1541 from mlocati/username-with-dots-and-underscores

Enable using dots and underscores in usernames
  • Loading branch information...
aembler committed Feb 19, 2014
2 parents 29498cd + b8f41d0 commit d8f401263ee6fc96feb67a7b01b3af791342760e
@@ -71,6 +71,11 @@ public function testUsername() {
$false[] = 'ab\'cdefg';
$false[] = 'ab"cdefg';
$false[] = 'ab\cdefg';
+ $false[] = '.abcdefg';
+ $false[] = '_abcdefg';
+ $false[] = 'abcdefg.';
+ $false[] = 'abcdefg_';
+ $false[] = '.abcdefg_';
$false[] = '!@#$%^&*()_+-=';
foreach ($false as $string) {
@@ -80,7 +85,9 @@ public function testUsername() {
$true = array();
$true[] = '1234567890';
$true[] = 'abcdefghijklmnopqrstuvwxyz';
-
+ $true[] = 'abc_defghijklmnopqrstuvwxyz';
+ $true[] = 'abc.defghijklmnopqrstuvwxyz';
+ $true[] = 'abc_def.ghi_jkl.mno_pqr.stu_vwx.yz';
foreach ($true as $string) {
$this->assertTrue($this->object->username($string));
}
@@ -44,9 +44,9 @@ public function view(){
if (strlen($username) >= USER_USERNAME_MINIMUM && !$valc->username($username)) {
if(USER_USERNAME_ALLOW_SPACES) {
- $this->error->add(t('A username may only contain letters, numbers and spaces.'));
+ $this->error->add(t('A username may only contain letters, numbers, spaces, dots (not at the beginning/end), underscores (not at the beginning/end).'));
} else {
- $this->error->add(t('A username may only contain letters or numbers.'));
+ $this->error->add(t('A username may only contain letters numbers, dots (not at the beginning/end), underscores (not at the beginning/end).'));
}
}
@@ -68,7 +68,8 @@ public function password($pass) {
/**
* Returns true if this is a valid username.
- * Valid usernames can only contain letters, numbers and optionally single spaces
+ * Valid usernames can only contain letters, numbers, dots (only in the middle), underscores (only in the middle) and optionally single spaces
+ * @return bool
*/
public function username($username) {
$username = trim($username);
@@ -78,16 +79,23 @@ public function username($username) {
if (strlen($username) > USER_USERNAME_MAXIMUM) {
return false;
}
+ $rxBoundary = '[A-Za-z0-9]';
if(USER_USERNAME_ALLOW_SPACES) {
- $resp = preg_match("/[^A-Za-z0-9 ]/", $username);
- } else {
- $resp = preg_match("/[^A-Za-z0-9]/", $username);
+ $rxMiddle = '[A-Za-z0-9_. ]';
}
-
- if ($resp > 0) {
- return false;
+ else {
+ $rxMiddle = '[A-Za-z0-9_.]';
+ }
+ if(strlen($username) < 3) {
+ if(!preg_match('/^' . $rxBoundary . '+$/', $username)) {
+ return false;
+ }
+ }
+ else {
+ if(!preg_match('/^' . $rxBoundary . $rxMiddle . '+'. $rxBoundary . '$/', $username)) {
+ return false;
+ }
}
-
return true;
}

0 comments on commit d8f4012

Please sign in to comment.