Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Regular Expression Help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rhkodiak - 09 Jan 2008 22:24 GMT
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;
}
KH - 10 Jan 2008 00:31 GMT
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;
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.