Intervention\Validation\Validator::isUsername PHP Method

isUsername() public static method

Checks if value is Username
public static isUsername ( mixed $value ) : boolean
$value mixed
return boolean
    public static function isUsername($value)
    {
        // username consists of alpha-numeric (a-z, A-Z, 0-9), underscores
        // starts with a alpha letter
        // and has minimum of 3 character and maximum of 20 characters
        $pattern = '/^[a-z][a-z\\d_]{2,20}$/i';
        return (bool) preg_match($pattern, $value);
    }

Usage Example

 public function testValidateUsername()
 {
     $usernames = array('tester', 'test', 'test_', 'mr_freeze', 'r00t', 'The_quick_brown_foXXX');
     foreach ($usernames as $name) {
         $this->assertTrue(Validator::isUsername($name));
     }
     $no_usernames = array('mr.freeze', 'mr freeze', 'mr-freeze', '1337', '-91819', '&nbsp;', '<html></html>', '-_homer_-', '_test_', '04420', '', ' ', 'array()', 'x', '$234_&', '?test=1', '€uro', 'SupersupersupersupersupersupersupersupersupersupersuperMan');
     foreach ($no_usernames as $username) {
         $this->assertFalse(Validator::isUsername($username));
     }
 }