16 lines
733 B
JavaScript
16 lines
733 B
JavaScript
// Keep legacy printable names working, while excluding whitespace, controls,
|
|
// path separators, and URL delimiters that are unsafe/confusing in identifiers.
|
|
const USERNAME_PATTERN = /^[^\p{C}\s\\/@?#%]{3,64}$/u;
|
|
|
|
export function normalizeUsername(value) {
|
|
return String(value || "").normalize("NFKC").trim();
|
|
}
|
|
|
|
export function usernameError(username, label = "Username") {
|
|
if (!username) return `${label} is required`;
|
|
if (username.length < 3) return `${label} must be at least 3 characters`;
|
|
if (username.length > 64) return `${label} must be at most 64 characters`;
|
|
if (!USERNAME_PATTERN.test(username)) return `${label} may contain letters, numbers, dots, underscores, and hyphens only`;
|
|
return null;
|
|
}
|