133

How can I validate a string using Regular Expressions to only allow alphanumeric characters in it?

(I don't want to allow for any spaces either).

yoozer8
  • 7,361
  • 7
  • 58
  • 93
mrblah
  • 99,669
  • 140
  • 310
  • 420
  • RegEx is slower than direct validation. If you write an extension method for strings, the check can be built in. You could also use one that's already written such as the [Extensions.cs](https://www.nuget.org/packages/Extensions.cs) NuGet package that makes it as simple as: For example: "abcXYZ".IsAlphabetic() will return True whereas "abc123".IsAlphabetic() will return False. – Cornelius J. van Dyk Jan 20 '21 at 13:32

12 Answers12

239

In .NET 4.0 you can use LINQ:

if (yourText.All(char.IsLetterOrDigit))
{
    //just letters and digits.
}

yourText.All will stop execute and return false the first time char.IsLetterOrDigit reports false since the contract of All cannot be fulfilled then.

Note! this answer do not strictly check alphanumerics (which typically is A-Z, a-z and 0-9). This answer allows local characters like åäö.

Update 2018-01-29

The syntax above only works when you use a single method that has a single argument of the correct type (in this case char).

To use multiple conditions, you need to write like this:

if (yourText.All(x => char.IsLetterOrDigit(x) || char.IsWhiteSpace(x)))
{
}
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • How does this perform compared to the regular expression? I assume it's as fast or faster. – crush Nov 24 '14 at 22:23
  • 1
    I would be surprised if it's not a lot faster. No regex to be compiled or evaluated, just a simple comparison. – jgauffin Nov 25 '14 at 06:17
  • 3
    Won't this fail if you want to make sure yourText is definitely alphanumeric? It could be either all digits or all alphabets but still satisfy this condition. – itsbalur Nov 05 '15 at 16:33
  • This checks if each character is a letter or digit, but how does it check if the string is both a combination of letters and digits? – Thabiso Mofokeng Feb 25 '16 at 08:58
  • 2
    @itsbalur: Yes, but that was not the question. – jgauffin Feb 25 '16 at 09:03
  • 4
    I think this answer is just plain wrong, assuming alphanumeric set is A-Z,a-z and 0-9 because this covers the whole range of Unicode letters and digits, which including non-Latin characters as well. For example, `char.IsLetterOrDigit('ก')` will return `true`. http://csharppad.com/gist/f96a6062f9f8f4e974f222ce313df8ca – tia Nov 16 '16 at 08:28
  • Do you think that all answers on stackoverflow are correct for all readers? Or are you saying that the ones asking questions always define the questions without ambiguity? What I mean is that many do use this answer since they ***want*** to allow local letters. If not they pick the accepted answer. However, your point is valid and I've updated the answer to clarify that. thanks! – jgauffin Nov 16 '16 at 17:44
  • Any way to also include white space? `char.IsWhiteSpace` is there. So can we use multiple conditions (`AND` or `OR`) something like: `yourText.All(char.IsLetterOrDigit || char.IsWhiteSpace)`? – Himanshu Jan 28 '18 at 08:36
  • 1
    @hims056: `yourText.All(x=> char.IsLetterOrDigit(x) || char.IsWhiteSpace(x))`. The shorthand (as in the answer only works for a single methodcall) – jgauffin Jan 28 '18 at 13:16
197

Use the following expression:

^[a-zA-Z0-9]*$

ie:

using System.Text.RegularExpressions;

Regex r = new Regex("^[a-zA-Z0-9]*$");
if (r.IsMatch(SomeString)) {
  ...
}
software_writer
  • 3,941
  • 9
  • 38
  • 64
cletus
  • 616,129
  • 168
  • 910
  • 942
35

You could do it easily with an extension function rather than a regex ...

public static bool IsAlphaNum(this string str)
{
    if (string.IsNullOrEmpty(str))
        return false;

    for (int i = 0; i < str.Length; i++)
    {
        if (!(char.IsLetter(str[i])) && (!(char.IsNumber(str[i]))))
            return false;
    }

    return true;
}

Per comment :) ...

public static bool IsAlphaNum(this string str)
{
    if (string.IsNullOrEmpty(str))
        return false;

    return (str.ToCharArray().All(c => Char.IsLetter(c) || Char.IsNumber(c)));
}
JP Alioto
  • 44,864
  • 6
  • 88
  • 112
  • 2
    It may be a matter of taste, but I would express the loop as "foreach (char c in str) {...}". Whether an empty string is to be considered as OK depends on the application, so I would take this out. I also wouldn't pad 6 empty lines into such a trivial routine, but I guess that's C#/Java/C++ style -- the coders seem to be paid by screen real estate. Anyway, this is the right direction, so +1. – Svante Jun 26 '09 at 00:40
  • 3
    I'd think we'd want to use IsDigit in this situation, rather than IsNumber -- IsNumber will return true for digits, or for things that *look* like numbers (fractions, Roman numerals, etc.; see http://msdn.microsoft.com/en-us/library/yk2b3t2y.aspx). Given that, and if one was feeling particularly evil, one could compress the contents of IsAlphaNum even further: return string.IsNullOrEmpty(str) ? false : str.ToCharArray().All(Char.IsLetterOrDigit); – stack Feb 13 '10 at 00:07
  • 4
    Note that Char.IsLetter will evaluate to true for "letters" other than a-zA-Z. For example, Japanese あ, Chinese 的, Korean 한 etc are considered to be Unicode "letters". If this is your intention, then fine, but judging from the various regex expressions in the other answers, this is likely not what most considered to be alpha[numeric]. – Dono Feb 20 '14 at 05:57
  • In my case, besides IsLetter and IsNumber I also needed IsWhiteSpace so I added it into your code and it worked perfectly! – Ben Junior Nov 20 '16 at 16:29
  • use `char.IsLetterOrDigit` instead IsLetter+IsNumber – nick_n_a Oct 30 '18 at 08:27
17

While I think the regex-based solution is probably the way I'd go, I'd be tempted to encapsulate this in a type.

public class AlphaNumericString
{
    public AlphaNumericString(string s)
    {
        Regex r = new Regex("^[a-zA-Z0-9]*$");
        if (r.IsMatch(s))
        {
            value = s;                
        }
        else
        {
            throw new ArgumentException("Only alphanumeric characters may be used");
        }
    }

    private string value;
    static public implicit operator string(AlphaNumericString s)
    {
        return s.value;
    }
}

Now, when you need a validated string, you can have the method signature require an AlphaNumericString, and know that if you get one, it is valid (apart from nulls). If someone attempts to pass in a non-validated string, it will generate a compiler error.

You can get fancier and implement all of the equality operators, or an explicit cast to AlphaNumericString from plain ol' string, if you care.

kyoryu
  • 12,848
  • 2
  • 29
  • 33
8

I needed to check for A-Z, a-z, 0-9; without a regex (even though the OP asks for regex).

Blending various answers and comments here, and discussion from https://stackoverflow.com/a/9975693/292060, this tests for letter or digit, avoiding other language letters, and avoiding other numbers such as fraction characters.

if (!String.IsNullOrEmpty(testString)
    && testString.All(c => Char.IsLetterOrDigit(c) && (c < 128)))
{
    // Alphanumeric.
}
Community
  • 1
  • 1
goodeye
  • 2,389
  • 6
  • 35
  • 68
5

^\w+$ will allow a-zA-Z0-9_

Use ^[a-zA-Z0-9]+$ to disallow underscore.

Note that both of these require the string not to be empty. Using * instead of + allows empty strings.

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
3

Same answer as here.

If you want a non-regex ASCII A-z 0-9 check, you cannot use char.IsLetterOrDigit() as that includes other Unicode characters.

What you can do is check the character code ranges.

  • 48 -> 57 are numerics
  • 65 -> 90 are capital letters
  • 97 -> 122 are lower case letters

The following is a bit more verbose, but it's for ease of understanding rather than for code golf.

    public static bool IsAsciiAlphaNumeric(this string str)
    {
        if (string.IsNullOrEmpty(str))
        {
            return false;
        }

        for (int i = 0; i < str.Length; i++)
        {
            if (str[i] < 48) // Numeric are 48 -> 57
            {
                return false;
            }

            if (str[i] > 57 && str[i] < 65) // Capitals are 65 -> 90
            {
                return false;
            }

            if (str[i] > 90 && str[i] < 97) // Lowers are 97 -> 122
            {
                return false;
            }

            if (str[i] > 122)
            {
                return false;
            }
        }

        return true;
    }
Douglas Gaskell
  • 9,017
  • 9
  • 71
  • 128
  • That's a lot of magic numbers... but probably the most efficient and accurate. – WesFanMan Jan 10 '23 at 02:46
  • What is the point of the comment? It's somewhat questionable that these qualify as true "magic numbers". They are single use, are constants rooted in the ASCII standard, are well known, the context is obvious, and the scope is encapsulated. Additionally, you'll find that direct number references become more common the less abstract the code is, especially when dealing with specs & standards, ofc if you're writing an ASCII library then having named references becomes far more valuable. – Douglas Gaskell Jan 11 '23 at 18:52
  • The point is using numbers explicitly rather than consts is generally accepted as poor practice. Code is less readable in this case. What do the numbers mean? Just give them a name. – WesFanMan Mar 06 '23 at 16:27
2

In order to check if the string is both a combination of letters and digits, you can re-write @jgauffin answer as follows using .NET 4.0 and LINQ:

if(!string.IsNullOrWhiteSpace(yourText) && 
yourText.Any(char.IsLetter) && yourText.Any(char.IsDigit))
{
   // do something here
}
Thabiso Mofokeng
  • 681
  • 9
  • 20
0

Based on cletus's answer you may create new extension.

public static class StringExtensions
{        
    public static bool IsAlphaNumeric(this string str)
    {
        if (string.IsNullOrEmpty(str))
            return false;

        Regex r = new Regex("^[a-zA-Z0-9]*$");
        return r.IsMatch(str);
    }
}
Alexa Adrian
  • 1,778
  • 2
  • 23
  • 38
0

While there are many ways to skin this cat, I prefer to wrap such code into reusable extension methods that make it trivial to do going forward. When using extension methods, you can also avoid RegEx as it is slower than a direct character check. I like using the extensions in the Extensions.cs NuGet package. It makes this check as simple as:

  1. Add the https://www.nuget.org/packages/Extensions.cs package to your project.
  2. Add "using Extensions;" to the top of your code.
  3. "smith23".IsAlphaNumeric() will return True whereas "smith 23".IsAlphaNumeric(false) will return False. By default the .IsAlphaNumeric() method ignores spaces, but it can also be overridden as shown above. If you want to allow spaces such that "smith 23".IsAlphaNumeric() will return True, simple default the arg.
  4. Every other check in the rest of the code is simply MyString.IsAlphaNumeric().
0

12 years and 7 months later, if anyone comes across this article nowadays.

Compiled RegEx actually has the best performance in .NET 5 and .NET 6

Please look at the following link where I compare several different answers given on this question. Mainly comparing Compiled RegEx, For-Loops, and Linq Predicates: https://dotnetfiddle.net/WOPQRT

Notes: As stated, this method is only faster in .NET 5 and .NET 6.

.NET Core 3.1 and below show RegEx being the slowest.

Regardless of the version of .NET, the For-Loop method is consistently faster than the Linq Predicate.

Saturn K
  • 2,705
  • 4
  • 26
  • 38
-8

I advise to not depend on ready made and built in code in .NET framework , try to bring up new solution ..this is what i do..

public  bool isAlphaNumeric(string N)
{
    bool YesNumeric = false;
    bool YesAlpha = false;
    bool BothStatus = false;


    for (int i = 0; i < N.Length; i++)
    {
        if (char.IsLetter(N[i]) )
            YesAlpha=true;

        if (char.IsNumber(N[i]))
            YesNumeric = true;
    }

    if (YesAlpha==true && YesNumeric==true)
    {
        BothStatus = true;
    }
    else
    {
        BothStatus = false;
    }
    return BothStatus;
}
Draken
  • 3,134
  • 13
  • 34
  • 54
  • 2
    Could you please add some explanation to your code, just dumping code is generally frowned upon here – Draken May 06 '16 at 13:17
  • Also they asked for regular expressions, this is not regular expressions – Draken May 06 '16 at 13:17
  • Thanks for comment and observation..as i advised i have my own approach to write code . – Mahdi Al Aradi May 06 '16 at 13:30
  • 5
    Your comment about not relying on pre build code in .Net makes little sense, surely if you shouldn't rely on pre-build code, you shouldn't use the `char.IsNumber()` method as that is pre-built code? – Draken May 06 '16 at 13:35
  • Thanks Again , however the way that has been taken developer is going to make them weaker than stronger , the more they write the more they will be having more q without answer . the best way to go back and write in lower level ..and this can be educated in steps .In all cases this was my first post i believe ..hope it won’t my last – Mahdi Al Aradi May 06 '16 at 13:55
  • 4
    This code is a great example of why reinventing it yourself is such a bad idea - it's doesn't actually do what you wanted to do! (The string "@1a" will return true incorrectly, the string "a" will return false) – Flexo Aug 05 '16 at 06:07
  • It will also return `true` simply if the last character in the string is alphanumeric since it never breaks out of the loop on a `false` condition – BackDoorNoBaby Aug 10 '16 at 21:27
  • In fact, it will just return `true` if ANY character in the string is alphanumeric – BackDoorNoBaby Aug 10 '16 at 21:40