Files

32 lines
874 B
PHP
Raw Permalink Normal View History

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