none
How to check if the text on a textbox contains any character less or higher than a character? RRS feed

  • Question

  • Hi.

    I need to check if a textbox contains a non integer character. I found the method textbox.text.contains but it doesn't help me because I don't see the way to work with the character's value in ascii.

    I mean something like this:

    if (nameOfTextBox.text.contains(anyCharacterAscii>57)||nameOfTextBox.text.contains(anyCharacterAscii<48){

    }

    I tried to put the nameOfTextBox.text into a string, but I can't work with ascii neither.

    I know my example is not possible so if you can give me a lead to solve this, I would appreciate it.

    Sunday, December 31, 2017 6:30 PM

All replies

  • Hi.

    I need to check if a textbox contains a non integer character. I found the method textbox.text.contains but it doesn't help me because I don't see the way to work with the character's value in ascii.

    I mean something like this:

    if (nameOfTextBox.text.contains(anyCharacterAscii>57)||nameOfTextBox.text.contains(anyCharacterAscii<48){

    }

    I tried to put the nameOfTextBox.text into a string, but I can't work with ascii neither.

    I know my example is not possible so if you can give me a lead to solve this, I would appreciate it.

    PD: I could go character by character with substring and a for validating if it's not an integer, but I imagine that it exists an easier way.
    Sunday, December 31, 2017 6:31 PM
  • If you really want to analyse the codes, then try this:

    if( nameOfTextBox.Text.Any( c => c < 48 || c > 57 ) )
    {
       . . .
    }

    Also try:

    if( nameOfTextBox.Text.Any( c => ! char.IsDigit( c ) ) )
    {
       . . .
    }

    Can be done with Regular Expressions too:

    if( Regex.IsMatch( nameOfTextBox.Text, @"\P{Nd}" ) )
    {
       . . .
    }



    Sunday, December 31, 2017 7:00 PM
  • Hi Kvothe838,

    As a supplement, use the regex with the format below would be helpful as well.

      Regex regex = new Regex(@"\d+");
                Match match = regex.Match(textBox1.Text);
    
                if (!match.Success)
                {
                    MessageBox.Show("no number");
                }
                else
                {
                    MessageBox.Show("has numbers");
                }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, January 2, 2018 8:42 AM
    Moderator