locked
Validate Textbox with Regular Expressions RRS feed

  • Question

  • I had originally posted this in the Powershell forums since my entire script is in Powershell, but they had recommend I post it here.

    I want to restrict the characters that the user inputs into a textbox on a form I have created. I want it restricted from between 7 and 11 digits (0-9). I am trying to use either regular expressions or a MaskedTextBox. I had almost gotten it to work with a MaskedTextBox, but then how do I validate it? My impression was that it would check as soon as focus is lost but that was not the case.

    My knowledge of C# is very minimal so any dumbing down would be appreciated.

    Wednesday, April 11, 2012 1:19 PM

Answers

  • Here is a code for you:

    private void myTextBox_Leave(object sender, EventArgs e)
    {
        Regex pattern = new Regex("[0-9]{7,11}");
        if (pattern.IsMatch(myTextBox.Text))
        {
            MessageBox.Show("OK");
        }
        else
        {
            MessageBox.Show("Invalid input");
        }
    }

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Marked as answer by Lie You Tuesday, April 17, 2012 3:34 AM
    Wednesday, April 11, 2012 8:47 PM
  • I do not test this code yet, but i believe that might help you
    //allow only integer numbers
    
    string nume = @"^\d+$";
    
    //or
    
    string num2 = @"^[0-9]+$";
    Match match = Regex.Match("10", num2);
    
    Label1.Text = "invalid";
    
    if (match.Success)
    {
      Label1.Text = "valid";
    }

    • Marked as answer by Lie You Tuesday, April 17, 2012 3:35 AM
    Wednesday, April 11, 2012 1:33 PM
  • Or you even can try some logic looks like this

    string valueFromTextBox = txt.Text;
    
    if (Regex.IsMatch(valueFromTextBox , @"^[0-9]*$"))
    {
      //works
    }

    • Marked as answer by Lie You Tuesday, April 17, 2012 3:34 AM
    Wednesday, April 11, 2012 1:35 PM

All replies

  • I do not test this code yet, but i believe that might help you
    //allow only integer numbers
    
    string nume = @"^\d+$";
    
    //or
    
    string num2 = @"^[0-9]+$";
    Match match = Regex.Match("10", num2);
    
    Label1.Text = "invalid";
    
    if (match.Success)
    {
      Label1.Text = "valid";
    }

    • Marked as answer by Lie You Tuesday, April 17, 2012 3:35 AM
    Wednesday, April 11, 2012 1:33 PM
  • Or you even can try some logic looks like this

    string valueFromTextBox = txt.Text;
    
    if (Regex.IsMatch(valueFromTextBox , @"^[0-9]*$"))
    {
      //works
    }

    • Marked as answer by Lie You Tuesday, April 17, 2012 3:34 AM
    Wednesday, April 11, 2012 1:35 PM
  • Here is a code for you:

    private void myTextBox_Leave(object sender, EventArgs e)
    {
        Regex pattern = new Regex("[0-9]{7,11}");
        if (pattern.IsMatch(myTextBox.Text))
        {
            MessageBox.Show("OK");
        }
        else
        {
            MessageBox.Show("Invalid input");
        }
    }

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Marked as answer by Lie You Tuesday, April 17, 2012 3:34 AM
    Wednesday, April 11, 2012 8:47 PM