I need help with validating usernames using regular expressions. I
only want to allow numbers, letters, and the underscore as valid
characters for the username. I just cannot seem to find the solution,
but I believe I am close. Any help provided is appreciated! Here is
my code below which I am using to validate usernames.
public bool ValidateHandle(string handle)
{
bool retVal = false;
if(Regex.IsMatch(handle, "^[a-zA-Z0-9_]$"))
{
retVal = true;
}
return retVal;
}
Your regex matches only one character, you can use * to match zero or more
chars, + to match one or more, or {3,5} to match 3,4,or 5 chars (or any other
numbers). You can also use \w to match those particular characters:
public bool ValidateHandle(string handle)
{
return Regex.IsMatch(handle, "^\w{5,24}$"); // at least 5 chars, no more
than 24
}
HTH
> I need help with validating usernames using regular expressions. I
> only want to allow numbers, letters, and the underscore as valid
[quoted text clipped - 13 lines]
> return retVal;
> }