Regular Expression Help

  • Thread starter Thread starter rhkodiak
  • Start date Start date
R

rhkodiak

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top