Files
pterodactyl-panel/app/Rules/Username.php
T
6c0413e39b Start 2.0 development
Co-authored-by: Anthony <anthony@infraly.co>
Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
2026-10-05 18:10:18 -04:00

32 lines
874 B
PHP

<?php
declare(strict_types=1);
namespace Pterodactyl\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class Username implements ValidationRule
{
/**
* Regex to use when validating usernames.
*/
public const string VALIDATION_REGEX = '/^[a-z0-9]([\w\.-]+)[a-z0-9]$/';
/**
* Validate that a username contains only the allowed characters and starts/ends
* with alphanumeric characters.
*
* Allowed characters: a-z0-9_-.
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (is_string($value) && preg_match(self::VALIDATION_REGEX, mb_strtolower($value)) === 1) {
return;
}
$fail('The :attribute must start and end with alpha-numeric characters and contain only letters, numbers, dashes, underscores, and periods.');
}
}