Practical validation
Real validation is rarely one trick - it's the pieces from the last five
lessons, stacked. A username shape like "3 to 16 characters, must start with
a letter, everything after can be a letter, digit, or underscore" reads
directly as a regex once you know the vocabulary: an anchor to demand the
whole string, a class for the first character, \w for the rest, and a
quantifier for the count.
^[a-zA-Z]\w{2,15}$ - start, one letter, then 2 to 15 more word characters,
end. That's a minimum total length of 3 and a maximum of 16. Same idea as the
guide's "perfect email regex is a trap" warning: aim for a shape that's
good enough and readable, not one that tries to catch everything.
Your task: write isValidUsername(str), true if str is 3-16 characters,
starts with a letter, and contains only letters, digits, or underscores after
that.
You'll practice:
- Combining a character class, a quantifier, and anchors in one pattern
- Reading a multi-part regex left to right as a sentence
Related reading: The Core Toolkit →