Exercise: Strict Username Validator
Problem statement
A new gaming platform requires strict username creation. A valid username must not contain any spaces, @ symbols, or # symbols. You must build a validator that ensures a requested username adheres to these strict rules from start to finish.
Task requirements
Accept a string representing the requested username.
Validate the username using a regular expression.
Return
trueif the username consists only of permitted characters.Return
falseif the username contains any forbidden characters (spaces,@, or#) or if it is completely empty.
Constraints
Use the static
Regex.IsMatch()method to evaluate the string.Use the
^and$anchors to ensure your pattern applies to the entire string.Use a negated character class
[^...]to define the forbidden characters.Use the
+quantifier to ensure the username has at least one valid character.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Look at the email validation example from the lesson. The
[^@\s]syntax means not an@and not a whitespace character.You can add the
#character to that negated set to forbid it as well.Do not forget the
+operator so the regular expression engine knows it must match one or more of the valid characters.
Exercise: Strict Username Validator
Problem statement
A new gaming platform requires strict username creation. A valid username must not contain any spaces, @ symbols, or # symbols. You must build a validator that ensures a requested username adheres to these strict rules from start to finish.
Task requirements
Accept a string representing the requested username.
Validate the username using a regular expression.
Return
trueif the username consists only of permitted characters.Return
falseif the username contains any forbidden characters (spaces,@, or#) or if it is completely empty.
Constraints
Use the static
Regex.IsMatch()method to evaluate the string.Use the
^and$anchors to ensure your pattern applies to the entire string.Use a negated character class
[^...]to define the forbidden characters.Use the
+quantifier to ensure the username has at least one valid character.
Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.
Get hints
Look at the email validation example from the lesson. The
[^@\s]syntax means not an@and not a whitespace character.You can add the
#character to that negated set to forbid it as well.Do not forget the
+operator so the regular expression engine knows it must match one or more of the valid characters.