Restrict possible usernames final test

I can’t seem to pass the final test, all others pass. I have seen the solution requires an Or operator, why is this necessary? Can my code work without using it?

Your code so far


let username = "JackOfAllTrades";
let userCheck = /^[A-Za-z][A-Za-z]+\d*$/; // Change this line
let result = userCheck.test(username);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36 Edg/91.0.864.71

Challenge: Restrict Possible Usernames

Link to the challenge:

So most of the difficulty in this problem stems from:

Usernames have to be at least two characters long. A two-character username can only use alphabet letters as characters.

Now let’s pen up some representative usernames that would pass all of the tests.
AB
A97
AB97

And you can see you in the case of longer usernames, your regex should be able to account for usernames with only 1 character at the start. But when it gets short, down to the character limit, your regex should only accept those with 2 characters.

And to account for that one character, at the moment I can only think of using OR.
Something like this:

/^[a-z]{2,}\d*$|^[a-z]+\d{2,}$/i;

Your regex would reject usernames such as “A97” since it expects two characters at the start always.

Can you clarify whether the first half or second half of the regex is for the username with or without numbers? Still a little confused, sorry. Should we not have a expression to define the maximum number of characters rather than the minimum? So if the expression is 2 characters maximum they should not include numbers?

Hey, sorry for the late reply.

So the first half is for usernames that has at least two letters. This means as long as it is entirely followed by numbers, it does not matter how many numbers there are. It could be 0 numbers, (e.g. username: AJ), or 5 (e.g. DAVID12345). We don’t care, because we have set it so the minimum requirement of at least 2 letters is always met when this regex is matched.

Second half is to fix the issues of not matching otherwise legitimate usernames in the first half. Since the first half always need 2 letters to start the username, this means legitimate usernames like A97 would not be matched. So here we fix that, but also be mindful of not letting in wrong usernames such as A9, since if we have only two characters, then they both must be letters. We also know that it has to have at least one letter.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.