242

I have an input string and I want to verify that it contains:

  • Only letters or
  • Only letters and numbers or
  • Only letters, numbers or underscore

To clarify, I have 3 different cases in the code, each calling for different validation. What's the simplest way to achieve this in C#?

1
  • RegEx is slower than a direct character check. You can write an extension method or use one that's already written such as the Extensions.cs NuGet package that makes it as simple as: For example: "abcXYZ".IsAlphabetic() will return True whereas "abc123".IsAlphabetic() will return False. Commented Jan 20, 2021 at 13:12

13 Answers 13

361

Only letters:

Regex.IsMatch(input, @"^[a-zA-Z]+$");

Only letters and numbers:

Regex.IsMatch(input, @"^[a-zA-Z0-9]+$");

Only letters, numbers and underscore:

Regex.IsMatch(input, @"^[a-zA-Z0-9_]+$");
6
  • 37
    This assumes a latin alphabet, where Char.IsLetter allows for non-latin alphabets. Commented Jul 28, 2009 at 7:31
  • 7
    Regex.IsMatch(input, "^[a-z0-9]+$", RegexOptions.IgnoreCase);
    – Tom Fobear
    Commented Oct 10, 2011 at 20:05
  • 4
    @TomFobear: this is slower than explicitly specifying [a-zA-Z] Commented Oct 11, 2011 at 14:38
  • 32
    Just to save people like me from one more search, Regex is in the System.Text.RegularExpressions Namespace
    – Eric Barr
    Commented Mar 19, 2014 at 13:51
  • 1
    The answer provided by Muhammad Hasan Khan further down is worth looking at too. RegEx text comparison is slow. Commented Jul 17, 2020 at 15:01
311
bool result = input.All(Char.IsLetter);

bool result = input.All(Char.IsLetterOrDigit);

bool result = input.All(c=>Char.IsLetterOrDigit(c) || c=='_');
6
  • 7
    I would be curious to know which one faster yours (LINQ) or the one from Philippe Leybaert (Regex)?
    – nam
    Commented Dec 27, 2015 at 4:39
  • 10
    @nam Running a test against the CMU Pronunciation Dictionary (all letters only, average length 7.4 chars), this is 1.8x faster than compiled Regex for all letters, and 3x faster than compiled Regex for all letters with IgnoreCase option (!).
    – NetMage
    Commented May 15, 2018 at 18:11
  • 2
    The accepted answer uses uncompiled Regex, that will be even slower. I prefer the LINQ solution, thanks for posting it :) Commented Jun 26, 2020 at 7:38
  • 1
    Note: While this is certainly a good option, it will not return the same result as the RegEx-based approach. This will use unicode categories to determine what a "letter" is, so it will match stuff like Umlauts, accented characters and more.
    – diredev
    Commented Oct 26, 2021 at 8:32
  • 1
    For anyone searching for letters + spaces, use this: input.All(x => char.IsLetter(x) || x == ' '); Commented Mar 28, 2022 at 18:24
55

Letters only:

Regex.IsMatch(theString, @"^[\p{L}]+$");

Letters and numbers:

Regex.IsMatch(theString, @"^[\p{L}\p{N}]+$");

Letters, numbers and underscore:

Regex.IsMatch(theString, @"^[\w]+$");

Note, these patterns also match international characters (as opposed to using the a-z construct).

0
26

For those of you who would rather not go with Regex and are on the .NET 2.0 Framework (AKA no LINQ):

Only Letters:

public static bool IsAllLetters(string s)
{
    foreach (char c in s)
    {
        if (!Char.IsLetter(c))
            return false;
    }
    return true;
}

Only Numbers:

    public static bool IsAllDigits(string s)
    {
        foreach (char c in s)
        {
            if (!Char.IsDigit(c))
                return false;
        }
        return true;
    }

Only Numbers Or Letters:

    public static bool IsAllLettersOrDigits(string s)
    {
        foreach (char c in s)
        {
            if (!Char.IsLetterOrDigit(c))
                return false;
        }
        return true;
    }

Only Numbers Or Letters Or Underscores:

    public static bool IsAllLettersOrDigitsOrUnderscores(string s)
    {
        foreach (char c in s)
        {
            if (!Char.IsLetterOrDigit(c) && c != '_')
                return false;
        }
        return true;
    }
1
  • This is more readable to me than Regex. For IsAllLetters(string s), a shorthand way of doing this without a foreach statement could be return s.All(c => Char.IsLetter(c)). Note: this would require using System.Linq;
    – Ian Davis
    Commented Mar 11, 2022 at 19:52
9

I think is a good case to use Regular Expressions:

public bool IsAlpha(string input)
{
    return Regex.IsMatch(input, "^[a-zA-Z]+$");
}

public bool IsAlphaNumeric(string input)
{
    return Regex.IsMatch(input, "^[a-zA-Z0-9]+$");
}

public bool IsAlphaNumericWithUnderscore(string input)
{
    return Regex.IsMatch(input, "^[a-zA-Z0-9_]+$");
}
1
  • 2
    Regex has static methods for this Commented Jul 25, 2009 at 7:05
6

You can loop on the chars of string and check using the Char Method IsLetter but you can also do a trick using String method IndexOfAny to search other charaters that are not suppose to be in the string.

2

Iterate through strings characters and use functions of 'Char' called 'IsLetter' and 'IsDigit'.

If you need something more specific - use Regex class.

1

I haven't yet seen a solution using pattern matching:

public static bool ContainsOnlyLetters(this string input)
{
    bool isValid = true;

    for (int i = 0; isValid && i < input.Length; i++)
    {
        isValid &= input[i] is >= 'A' and <= 'Z' or >= 'a' and <= 'z';
    }

    return isValid;
}

or if you really really hate readable code:

public static bool ContainsOnlyLetters(this string input)
{
    bool isValid = true;
    for (int i = 0; i < input.Length && (isValid &= input[i] is >= 'A' and <= 'Z' or >= 'a' and <= 'z'); i++);
    return isValid;
}
1

I added a check so that I could only get the Alphabets and white spaces. You can repeat the for loop after the second if statement to validate the string again.

bool check = false;

Console.WriteLine("Please Enter the Name");
name=Console.ReadLine();

for (int i = 0; i < name.Length; i++)
{
    if (name[i]>='a' && name[i]<='z' || name[i]==' ')
    {
        check = true;
    }
    else
    {
        check = false;
        break;
    }

}

if (check==false)
{
    Console.WriteLine("Enter Valid Value");
    name = Console.ReadLine();
}
0

Recently, I made performance improvements for a function that checks letters in a string with the help of this page.

I figured out that the Solutions with regex are 30 times slower than the ones with the Char.IsLetterOrDigit check.

We were not sure that those Letters or Digits include and we were in need of only Latin characters so implemented our function based on the decompiled version of Char.IsLetterOrDigit function.

Here is our solution:

internal static bool CheckAllowedChars(char uc)
    {
        switch (uc)
        {
            case '-':
            case '.':
            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
            case 'F':
            case 'G':
            case 'H':
            case 'I':
            case 'J':
            case 'K':
            case 'L':
            case 'M':
            case 'N':
            case 'O':
            case 'P':
            case 'Q':
            case 'R':
            case 'S':
            case 'T':
            case 'U':
            case 'V':
            case 'W':
            case 'X':
            case 'Y':
            case 'Z':
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                return true;
            default:
                return false;
        }
    }

And the usage is like this:

 if( logicalId.All(c => CheckAllowedChars(c)))
 { // Do your stuff here.. }
0

Please find the method to validate if char is letter, number or space, otherwise attach underscore (Be free to modified according your needs)

public String CleanStringToLettersNumbers(String data)
{
    var result = String.Empty;

    foreach (var item in data)
    {
        var c = '_';

        if ((int)item >= 97 && (int)item <= 122 ||
            (int)item >= 65 && (int)item <= 90 ||
            (int)item >= 48 && (int)item <= 57 ||
            (int)item == 32)
        {
            c = item;
        }

        result = result + c;
    }

    return result;
}
0

Use this to scan each CHARACTER of the STRING.

You can add SWITCH STATEMENT after 1 IF STATEMENT if you want to include "many" characters aside from 'SPACE'.

string myInput = string.Empty;
bool check = false; 

// Loops the input module
while (check is false)
{
    Console.WriteLine("Enter Letters Only");
    myInput = Console.ReadLine();

    // Loops the SCANNING PROCCESS of each character of the string
    for (int i = 0; i < myInput.Length; i++)                  
    {
        // Prints current scanning proccess 1 by 1(character by character) inside the string
        Console.WriteLine("Checking Character \"{0}\" ",myInput[i]);
        
        // Letters only     
        if (Char.IsLetter(myInput[i]))
        {
            check = true;
        }
        
        // Includes "SPACE" character
        else if (myInput[i] == ' ')
        {
            check = true;
        }
        else
        {
            check = false;
            Console.WriteLine("wrong input. \"{0}\" is not a string", myInput[e]);
            Console.WriteLine("pls try again");
            // Exits from loop of scanning proccess due to unwanted input
            break;        
        }  
    }
    // Ends SCANNING of 1 CHARACTER inside the string
} 
Console.WriteLine("Input Approved: \"{0}\"", myInput);
Console.WriteLine("Press any key to exit");
Console.ReadKey();

OUTPUT: With FALSE Input

Enter Letters Only
Agent 47
Checking Character "A"
Checking Character "g"
Checking Character "e"
Checking Character "n"
Checking Character "t"
Checking Character " "
Checking Character "4"
Oops. "4" is not a Valid Input
pls try again

Enter Letters Only

OUTPUT: Without FALSE Input

Enter Letters Only
Agent Forty Seven
Checking Character "A"
Checking Character "g"
Checking Character "e"
Checking Character "n"
Checking Character "t"
Checking Character " "
Checking Character "F"
Checking Character "o"
Checking Character "r"
Checking Character "t"
Checking Character "y"
Checking Character " "
Checking Character "S"
Checking Character "e"
Checking Character "v"
Checking Character "e"
Checking Character "n"
Input Approved: "Agent Forty Seven"
Press any key to exit

Or SWITCH STATEMENT only, if you only want to include "some" CHARACTERS from LETTER category & "some" CHARATERS from DIGIT category.

switch (myInput[e])
{ 
    case 'a':
    case 'b':
    case 'c':
    case 'd': 
    case '1':
    case '2':
    case '3':
    case '!':
    case '@': 
    case '#':  
        check = true;
        break; 
    default:
        check = false;
        Console.WriteLine("Oops, \"{0}\" is not a string", myInput[i]);
        Console.WriteLine("pls try again\n");
        break;
}
if (check == false) break ;
2
  • If your last example you might as well use check = "abcd123!@#".Contains(myInput[e]). Commented Jan 17, 2022 at 6:46
  • Thank you @Enigmativity . I am just a newbie coder, I appreciate any feedbacks either good &bad feedbacks Commented Jan 17, 2022 at 7:10
-1
public static bool ContainsOnlyLetters(this string Foo) => Foo.All(Char.IsLetter);

public static bool OnlyAlphanumeric(this string Foo) => Foo.All(Char.IsLetterOrDigit);

public static bool IsAlphaNumericOrSpecificChars(this string Foo, char[] AcceptableChars = null)
{
    AcceptableChars ??= { '_' };
    return Foo.All(c => Char.IsLetterOrDigit(c) || AcceptableChars.Contains(c));
}
2
  • Your answer is almost the same as the accepted one: stackoverflow.com/a/1181488/876743
    – ademchenko
    Commented Jan 6 at 1:16
  • Thanks! The one you linked (that is very similar) is not the accepted one (probably why I didn't see it initially). Unless I'm reading it wrong, the accepted one uses Regex. Commented Feb 7 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.