.NET Framework Developer Center > .NET Development Forums > Regular Expressions > Need help with validating an alphanumeric string within a textbox control.
Ask a questionAsk a question
 

AnswerNeed help with validating an alphanumeric string within a textbox control.

  • Wednesday, October 28, 2009 5:52 PMWaffleMan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi everyone,

    I am developing a simple WinForms app for a presentation next week and need to validate that a particular string in a particular textbox is alphanumeric.  I have included the code from the button click event handler below:

            // Alphanumeric
            private void btnValidate4_Click(object sender, EventArgs e)
            {
                try
                {
                    if (txtAlphanumeric.Text == "")
                        MessageBox.Show("Please enter an alphanumeric string before clicking the 'Validate' button.", "Whoa there, pardner!!", MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
    
                    else
                    {
                        Regex rgxAlphanumeric = new Regex("^[a-zA-Z0-9]*$");
                        Match alphaNumericMatch = rgxAlphanumeric.Match(txtAlphanumeric.Text);
    
                        if (!alphaNumericMatch.Success)
                            MessageBox.Show("The string supplied does NOT match the specified pattern.", "Error!", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
    
                        else
                        {
                            MessageBox.Show("The supplied string matches the specified pattern.", "Success!", MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);
    
                        }
                    }
                }
    
                catch (Exception exc)
                {
                    MessageBox.Show(exc.ToString(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
    
                }
    
                finally
                {
                    ClearControls();
    
                }
            }
    
    I think the regular expression is written correctly but, when I pass in '111' or 'aaa' (for example), it tells me that string matches the pattern; which it doesn't.  Any elucidation/help is appreciated.

    Thanks,

    Brian

Answers

  • Wednesday, October 28, 2009 8:23 PMAhmad Mageed Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Your pattern behaves correctly and matches alphanumeric characters. "111" and "aaa" are valid matches. Perhaps you can tell us why you think they shouldn't match, and provide a sample of strings that you think should and should not match. Currently your pattern matches any letter from A-Z (lower- and upper-case) and any number from 0-9, and matches them zero or more times. So "111" matches since "1" is in the valid 0-9 range, and you are allowing zero or more repetitions of the pattern, and each "1" in "111" is valid (same concept for "aaa").
    Document my code? Why do you think it's called "code"?
    • Marked As Answer byWaffleMan Wednesday, October 28, 2009 9:08 PM
    •  

All Replies

  • Wednesday, October 28, 2009 8:23 PMAhmad Mageed Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Your pattern behaves correctly and matches alphanumeric characters. "111" and "aaa" are valid matches. Perhaps you can tell us why you think they shouldn't match, and provide a sample of strings that you think should and should not match. Currently your pattern matches any letter from A-Z (lower- and upper-case) and any number from 0-9, and matches them zero or more times. So "111" matches since "1" is in the valid 0-9 range, and you are allowing zero or more repetitions of the pattern, and each "1" in "111" is valid (same concept for "aaa").
    Document my code? Why do you think it's called "code"?
    • Marked As Answer byWaffleMan Wednesday, October 28, 2009 9:08 PM
    •  
  • Wednesday, October 28, 2009 9:08 PMWaffleMan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I suspected that the regex I used as correct, and you are right, '111' and 'aaa' are valid matches.  I suppose though - and this is just in my mind's eye - that when I hear 'alphanumeric' I think along the lines of 'a234kbkasd2', or something similar. 

    But, since this is going to be used for a one-off training session, I suppose I should just leave the code 'as-is' and call it good.

    Thanks for your response!

    --Brian