Validate Username (Rules + Edge Cases)
Arguments
name: string —The username to validate.
Returns
boolean — Solution idea
Validate step-by-step: type guard, length check, allowed characters, and underscore rules. This makes edge cases explicit and tests easy to reason about.
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.
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;
}
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
Forgetting to reject trailing underscores. Allowing uppercase letters or hyphens accidentally. Regex-only solutions that hide edge-case intent without tests.
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.
Boundary checks before regex for clarity. String methods for simple rules (endsWith, includes). Small, focused unit tests for each rule.
Common mistakes on this challenge
Username rule validation misses one or more constraints
Validation combines type, length, charset, start/end, and underscore sequence rules. Reject non-string inputs and enforce length bounds first. Enforce lowercase-start + allowed chars ( [a-z0-9_]).Reject trailing underscore and consecutive underscores.
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.