0

I have a method which allows me to reach competitor information which is saved in my class.

It already blocks empty inputs with a message. If the user inputs a number which is non-existent, the program blocks with another message.

I would like to implement a functionality where, if any form of text input is true, the input is classed as invalid so the user has to change their input to a valid value instead.

Here is my piece of code where I pick which competitor needs to be printed. I am new to stackoverflow so please let me know if more code needs to be shared by my side.

public void CompetitorToPrint(string NumberTextBoxInput)
{

    Skier searchCompetitor = ActiveLodge.FindCompetitor(NumberTextBoxInput);
    if (searchCompetitor == null)
        MessageBox.Show("Competitor number does not exist. Please try again. ");
    else
        EditCompetitor(searchCompetitor);
}
JsAndDotNet
  • 16,260
  • 18
  • 100
  • 123
Umut
  • 7
  • 5
  • 2
    Check this: [http://stackoverflow.com/questions/437882/what-is-the-c-sharp-equivalent-of-nan-or-isnumeric](http://stackoverflow.com/questions/437882/what-is-the-c-sharp-equivalent-of-nan-or-isnumeric) :) – DIEGO CARRASCAL Apr 25 '16 at 13:47

1 Answers1

0

There are many ways to do this, two that spring to mind are that you could loop over the characters in the box, or use regex

//Looping
bool textChar = false;
foreach(char letter in NumberTextBoxInput)
{
    if(!Char.IsNumber(letter))
    {
        textChar = true;
        break;
    }
}
if(textChar)
    MessageBox.Show("Please enter a valid number.");
else
{
    //Rest of method
}

Alternatively

//Regex
Regex reg = new Regex(@"\D");
if(reg.IsMatch(NumberTextBoxInput))
{
    MessageBox.Show("Please enter a valid number.");
}
else
{
    //Rest of method
}
Alfie Goodacre
  • 2,753
  • 1
  • 13
  • 26