Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • This is the only correct answer on this page. Please note, that this regex also permits a string that is completely empty. So I would replace the * with a +. The comma is not alphanumeric, so the name of the function is misleading. If you want a shorter version you can write: @"^[\w\s,]+$" Commented Dec 3, 2015 at 13:32
  • 1
    I added an answer that avoids regex, and only accepts a-z A-Z 0-0, unlike the majority of answers using the char functions. It's as simple as checking for character code ranges. Commented Mar 6, 2020 at 19:57
  • I just ran a test, and zipcode.Any(c => char.IsLetterOrDigit(c)); takes about 1/100 the time as the regex answer, and supports non-ascii characters. (at least with the sample zip code input. Commented Dec 4, 2020 at 19:59
  • @JamieF Note that char.IsLetterOrDigit() is not stable or reliable if there is any chance of receiving unicode input where the characters are unicode scalar values. Similarly it lets through non A-z characters from other languages. learn.microsoft.com/en-us/dotnet/api/… Commented Aug 10, 2021 at 20:32