Yevhen Klymentiev
dark
light
console
darkness
y.klymentiev@gmail.com
Reusable Snippets|Practical utility code for everyday use — custom-built and ready to share

isValidUsername

Validates whether a username meets common criteria.

TypeScript
Copied!
1/**
2 * Validates whether a username meets common criteria.
3 *
4 * Rules:
5 * - 3 to 20 characters long
6 * - Only letters, numbers, underscores, and dots
7 * - Cannot start or end with a dot or underscore
8 * - Cannot have consecutive dots or underscores
9 *
10 * @param username - The string to validate.
11 * @returns True if valid, false otherwise.
12 */
13export function isValidUsername(username: string): boolean {
14  if (!/^[a-zA-Z0-9._]{3,20}$/.test(username)) return false;
15  if (/^[._]/.test(username) || /[._]$/.test(username)) return false;
16  if (/(\.\.|__)/.test(username)) return false;
17  return true;
18}
  • Comprehensive Rule Enforcement

    Enforces multiple common constraints in a clear, layered manner-covering length, allowed characters, and structural rules.

  • Readable & Maintainable Logic

    Splits validation into small, expressive checks rather than a single complex regex, improving maintainability and clarity.

  • Performance-Efficient

    Uses simple and efficient regex checks with minimal overhead, ideal for real-time validation (e.g., on input change).

  • Minimizes User Input Errors

    Prevents common formatting mistakes like leading/trailing or repeated special characters early in the user experience.

Tests | Examples

TypeScript
Copied!
1test('valid usernames', () => {
2  expect(isValidUsername('user_name')).toBe(true);
3  expect(isValidUsername('user.name')).toBe(true);
4  expect(isValidUsername('user123')).toBe(true);
5  expect(isValidUsername('abc')).toBe(true);
6  expect(isValidUsername('user.name_99')).toBe(true);
7});
8
9test('invalid usernames - too short or long', () => {
10  expect(isValidUsername('ab')).toBe(false);
11  expect(isValidUsername('a'.repeat(21))).toBe(false);
12});
13
14test('invalid usernames - invalid characters', () => {
15  expect(isValidUsername('user-name')).toBe(false);
16  expect(isValidUsername('user@name')).toBe(false);
17  expect(isValidUsername('user name')).toBe(false);
18});
19
20test('invalid usernames - starts or ends with dot/underscore', () => {
21  expect(isValidUsername('_username')).toBe(false);
22  expect(isValidUsername('.username')).toBe(false);
23  expect(isValidUsername('username_')).toBe(false);
24  expect(isValidUsername('username.')).toBe(false);
25});
26
27test('invalid usernames - consecutive dots or underscores', () => {
28  expect(isValidUsername('user..name')).toBe(false);
29  expect(isValidUsername('user__name')).toBe(false);
30});

Common Use Cases

  • User Registration Forms

    Validate usernames during sign-up to prevent invalid or unsafe identifiers.

  • Profile Update Interfaces

    Re-validate modified usernames before saving changes.

  • Admin Dashboards

    Check for proper formatting when creating or editing users from an admin panel.

  • Pre-Validation for API Requests

    Sanitize and validate usernames before submitting to backend services or storing in a database.

Codebase: Utilities -> Validation -> isValidUsername | Yevhen Klymentiev