alix54
alix54

Reputation: 474

Check if string have uppercase, lowercase and number

I want to check if my string have Uppercase & LowerCase & Number

string myString = "Hello123";

if (myString haveUppercase && myString haveLowerCase && myString haveNumber)
{
    this.hide();
}
else
{
    MessageBox.Show("Error!");
}

Upvotes: 31

Views: 56786

Answers (7)

Cornelius J. van Dyk
Cornelius J. van Dyk

Reputation: 728

It looks like you're trying to do some strong password validation. 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.

Your example code would become as simple as:

string myString = "Hello123";

//if (myString haveUppercase && myString haveLowerCase && myString haveNumber)
if (myString.IsStrong(true, true, true, false))
//If you wanted to allow special characters and require 3 of the 4 it would simply be:
if (myString.IsStrong(3))
{
    this.hide();
}
else
{
    MessageBox.Show("Error!");
}

Upvotes: 0

user9667279
user9667279

Reputation: 7

#region check if there's upper case letter

        string Mystring = "SimpleWordforExAmple";
        char[] chars;
        char ch;
        int length = Mystring.Length;
        int cnt;
        int totalcntupper = 0;

        chars = Mystring.ToCharArray(0, length);

        Console.WriteLine("Sample words with capital letters : {0} ", Mystring);
        for (cnt = 0; cnt < length;cnt ++)
        {
            ch = chars[cnt];


            if (char.IsUpper(ch))
            {
                Console.WriteLine("Capital letter : #{0}", ch);
                totalcntupper++;

            }


        }

        Console.WriteLine("Count of capital letter(s) : # {0}", totalcntupper);
        Console.ReadLine();








        #endregion

i hope you got an idea. thanks

Upvotes: 0

vasantharajan pandian
vasantharajan pandian

Reputation: 11

We can use Extension Method for Alphanumeric as group like below

(Also we can use different Extension method for Small Letters, Capital Letters and Numbers separately if we need)

static class StringExtension
{

    public static bool IsAlphaNumeric(this string strToCheck)
    {
        Regex rg = new Regex(@"^[a-zA-Z0-9\s,]*$");
        return rg.IsMatch(strToCheck);

    }

 }

We can use the above Extension Method like below :

 {

 string strValue = "vasanth";

 strValue.IsAlphaNumeric();  //return true

 string strValue1 = "vasanth!";

 strValue.IsAlphaNumeric();  //return false

  }

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460158

For the sake of completess, the classic, non-LINQ way to achieve this:

public static bool HasUpperLowerDigit(string text)
{
    bool hasUpper = false; bool hasLower = false; bool hasDigit = false;
    for (int i = 0; i < text.Length && !(hasUpper && hasLower && hasDigit); i++)
    {
        char c = text[i];
        if (!hasUpper) hasUpper = char.IsUpper(c);
        if (!hasLower) hasLower = char.IsLower(c);
        if (!hasDigit) hasDigit = char.IsDigit(c);
    }
    return hasUpper && hasLower && hasDigit;
}

It is more efficient because it loops every character only once whereas the LINQ approaches need three enumerations.

Upvotes: 8

Amir Popovich
Amir Popovich

Reputation: 29836

if (myString.Any(ch => char.IsUpper(ch) &&
    myString.Any(ch => char.IsLower(ch) &&
    myString.Any(ch => char.IsDigit(ch))
    { 
      this.hide();
    }
    else
    {
        MessageBox.Show("Error!");
    }

Upvotes: 9

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98750

How about?

if(myString.Any(char.IsLower) && myString.Any(char.IsUpper) && myString.Any(char.IsDigit))

Upvotes: 7

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101701

You can use char's methods with LINQ:

if (myString.Any(char.IsUpper) &&
    myString.Any(char.IsLower) &&
    myString.Any(char.IsDigit))

Upvotes: 64

Related Questions