FrontendAtlas
Interview Questions
Home>JavaScript interview questions> JavaScript coding challenges >Validate Username (Rules + Edge Cases)

Validate Username (Rules + Edge Cases)

easy
By FrontendAtlas Editorial · Updated Feb 3, 2026
Implement validateUsername(name) to enforce common username rules and handle edge cases safely. Rules: 3–16 chars, lowercase letters/digits/underscore only, must start with a letter, no consecutive underscores, and no trailing underscore.

Arguments

  • name: string — The username to validate.

Returns

boolean — True if valid; otherwise false.
Examples
validateUsername('alice_1') // => true

validateUsername('2cool')   // => false (must start with a letter)

validateUsername('bob__x')  // => false (no consecutive underscores)

Solution

Overview

Validate step-by-step: type guard, length check, allowed characters, and underscore rules. This makes edge cases explicit and tests easy to reason about.

1

Approach 1: Explicit checks (clear and testable)

Steps:

1) Ensure input is a string.

2) Check length between 3 and 16.

3) Verify allowed characters (lowercase letters, digits, underscore).

4) Ensure it starts with a letter.

5) Reject consecutive underscores and trailing underscore.

Complexity: O(n) time, O(1) extra space.

export default function validateUsername(name) {
  if (typeof name !== 'string') return false;
  const len = name.length;
  if (len < 3 || len > 16) return false;
  if (!/^[a-z][a-z0-9_]*$/.test(name)) return false;
  if (name.endsWith('_')) return false;
  if (name.includes('__')) return false;
  return true;
}
export default function validateUsername(name: string): boolean {
  if (typeof name !== 'string') return false;
  const len = name.length;
  if (len < 3 || len > 16) return false;
  if (!/^[a-z][a-z0-9_]*$/.test(name)) return false;
  if (name.endsWith('_')) return false;
  if (name.includes('__')) return false;
  return true;
}
2

Approach 2: Single regex (compact)

A single regex can encode all rules. It is shorter but harder to maintain. Keep tests strong so refactors do not break edge cases.

export default function validateUsername(name) {
  return typeof name === 'string' && /^[a-z](?!.*__)[a-z0-9_]{1,14}[a-z0-9]$/.test(name);
}
export default function validateUsername(name: string): boolean {
  return typeof name === 'string' && /^[a-z](?!.*__)[a-z0-9_]{1,14}[a-z0-9]$/.test(name);
}

Notes & Pitfalls

Pitfalls
  • Forgetting to reject trailing underscores.
  • Allowing uppercase letters or hyphens accidentally.
  • Regex-only solutions that hide edge-case intent without tests.
Edge cases
  • Length boundaries: 3 and 16 are valid; 2 and 17 are invalid.
  • Leading digit is invalid even if the rest is valid.
  • Consecutive underscores are invalid anywhere in the string.
Techniques
  • Boundary checks before regex for clarity.
  • String methods for simple rules (endsWith, includes).
  • Small, focused unit tests for each rule.

Resources

  • MDN – Regular expressions

Similar questions

Safe JSON Parse (Fallback on Error)easySanitize href URL (Block javascript: XSS)intermediateFormat Date in Time Zone (YYYY-MM-DD)intermediate

Guides

Frontend interview preparation guideGuideFrontend coding interview questions and prep guideBlueprintJavaScript Problems That Actually Show UpBlueprintBuild Great UI in 60 MinutesBlueprint

Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.

Open frontend interview questionsBrowse JavaScript interview questionsOpen Essential 60Open Machine Coding HubOpen Frontend Coding ChallengesOpen System DesignOpen Interview Prep GuideOpen System Design BlueprintOpen Framework Prep PathsOpen Study PlansOpen JavaScript mastery study planOpen Company PrepOpen JavaScript Framework Prep Guide
↗Incidents hub
87 / 88