Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Rules/Username.php
7432 views
1
<?php
2
3
namespace Pterodactyl\Rules;
4
5
use Illuminate\Contracts\Validation\Rule;
6
7
class Username implements Rule
8
{
9
/**
10
* Regex to use when validating usernames.
11
*/
12
public const VALIDATION_REGEX = '/^[a-z0-9]([\w\.-]+)[a-z0-9]$/';
13
14
/**
15
* Validate that a username contains only the allowed characters and starts/ends
16
* with alphanumeric characters.
17
*
18
* Allowed characters: a-z0-9_-.
19
*
20
* @param string $attribute
21
*/
22
public function passes($attribute, $value): bool
23
{
24
return preg_match(self::VALIDATION_REGEX, mb_strtolower($value));
25
}
26
27
/**
28
* Return a validation message for use when this rule fails.
29
*/
30
public function message(): string
31
{
32
return 'The :attribute must start and end with alpha-numeric characters and
33
contain only letters, numbers, dashes, underscores, and periods.';
34
}
35
36
/**
37
* Convert the rule to a validation string. This is necessary to avoid
38
* issues with Eloquence which tries to use this rule as a string.
39
*/
40
public function __toString(): string
41
{
42
return 'p_username';
43
}
44
}
45
46