1

This is the code I am trying:

string MatchNumberPattern = "^[a-zA-Z0-9]*$";

if (!Regex.IsMatch(cueTextBox9.Text, MatchNumberPattern))
{
    MessageBox.Show("Enter 8 Space Alphanumeric BT ID only");
    cueTextBox9.Text = String.Empty;
}
else
{
    do something();
}

It's accepting aaaaaaaa, but I want a combination of both alpha and numbers like aaaa1234.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • 1
    Of course it's taking `aaaaaaaa`, you specified it to take anything that is a-z or A-Z or 0-9 any number of times. So it would take `aaabbbjfgkgAJFFJL0142342` as well. What would you like to do? If you only want to match numbers: `^[0-9]*$` or `^[0-9]+$` if empty string is not ok. Instead of `[0-9]` you can use `\d` as a short-hand, but first please clarify what you want :) – Szabolcs Dézsi Feb 13 '16 at 13:16
  • i want to accept only alphanumeric ,not alpha only or not numbers only.. – Deep Siddharth Verma Feb 13 '16 at 13:20
  • Alphanumeric means "Alphanumeric is a combination of alphabetic and numeric characters..." (quote from wiki), so it means letters and digits. This is what you're already doing here. – Szabolcs Dézsi Feb 13 '16 at 13:21
  • dude, i am saying i want both alpha and numerics in it... both in one string then it will be acceptable.otherwise not – Deep Siddharth Verma Feb 13 '16 at 13:23
  • Use `@"^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]*$"`. Is that what you need? – Wiktor Stribiżew Feb 13 '16 at 13:26
  • thanks man. it worked. :) @WiktorStribiżew – Deep Siddharth Verma Feb 13 '16 at 13:29
  • 1
    For future reference: "I would like my regular expression to only match inputs which contain both letters and digits at the same time". And don't "dude" me. – Szabolcs Dézsi Feb 13 '16 at 13:30
  • that what alphanumeric only means. by the way thanks for the advice :) @SzabolcsDézsi – Deep Siddharth Verma Feb 13 '16 at 13:33
  • I am reopening this question since the answer in the [Regex for alphanumeric with at least 1 number and 1 character](http://stackoverflow.com/questions/7684815/regex-for-alphanumeric-with-at-least-1-number-and-1-character) is a trash "why-not-use-this" answer without any explanation. – Wiktor Stribiżew Feb 15 '16 at 22:26

3 Answers3

2

To require both a letter and a digit to appear in the input, you need positive lookaheads:

@"^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]*$"
    ^^^^^^^^^^^^^^^^^^^^^^^^

See the regex demo

The (?=.*[a-zA-Z]) makes sure there is a letter and (?=.*[0-9]) makes sure there is a digit in the input string.

Since you are taking the input from a single-line text box, it is safe to use . in the lookahead. As an alternative, you can use @"^(?=[^a-zA-Z]*[a-zA-Z])(?=[^0-9]*[0-9])[a-zA-Z0-9]*$" (based on the principle of contrast).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
2

You can use a lookahead to check for a digit and match an alpha before the rest.

^(?i)(?=\D*\d)\d*[A-Z][A-Z\d]*$
  • ^ start of string
  • (?i) flag for caseless matching
  • (?=\D*\d) loook ahead for \D* any amount of non-digits followed by \d digit
  • if succeeds match \d*[A-Z] any amount of digits followed by alpha
  • [A-Z\d]* match any amount of alphanumeric characters until
  • $ end of string

See demo at regex101

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
0
  async Task<string> CheckPasswordAgainstPasswordSettings(string password)
        {
            Regex rxUpper= new Regex(@"[A-Z]");
            Regex rxLower = new Regex(@"[a-z]");



            var passwordcomplexityrulesetttings=  (await _PasswordComplexityRuleService.All()).First();
          if(passwordcomplexityrulesetttings==null)
            {
                return "password valid";
            }
          if(passwordcomplexityrulesetttings.MinLength> password.Length)
            {
                return "password is not of minimum length";

            }
            if (passwordcomplexityrulesetttings.MustContainLettersNumbers)
            {
                if(password.Any(a=>!char.IsDigit(a)&&!char.IsLetter(a)) && !passwordcomplexityrulesetttings.MustContainSpecialCharacters)
                
                {
                    return "password must contain letters and numbers";
                }


            }

            if (passwordcomplexityrulesetttings.MustContainSpecialCharacters)
            {

                if (!password.Any(a => !char.IsDigit(a) && !char.IsLetter(a)))

                {
                    return "password must contain special characters";
                }


            }
            if (passwordcomplexityrulesetttings.MustContainUpperLower)
            {

                if (!rxLower.Match(password).Success||!rxUpper.Match(password).Success)

                {
                    return "password must contain upper and lower case";
                }


            }
            
            return "password valid";

        }