1

I'm currently having some issues with error checking on a textbox. It holds a variable (called Price) as Double. Its purpose is to take a number in, and when a button is clicked it adds it to a running total displayed in a different textbox.

Now I have an error when checking if the textbox is empty using:

!string.IsNullOrWhiteSpace(txtAddItem.Text)

However, I'm unsure of how to error check if a string or character other than a number has been entered. Any ideas are appreciated.

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
  • Possible duplicate of [Identify if a string is a number](https://stackoverflow.com/questions/894263/identify-if-a-string-is-a-number) – djv Feb 27 '19 at 15:42

3 Answers3

3

Method 1: RegEx

You should try to use a regular expression. Regular expressions (Regex in short) are used to match strings against patterns. For example, if you only want to allow integers:

Regex r = new Regex(@"^[0-9]+$")

The Regex class has an .IsMatch(string s) method, where s is the string you want to test against the pattern.

Method 2: try-catch and Parse()

Another way to do it, which might be a bit more beginner-friendly, is a try-catch block. (I am assuming your TextBox's name is TextBox1 and you store the sum value in a runningSum variable.)

try {
    double x = double.Parse(TextBox1.Text);
    runningSum += x;
catch (ArgumentException ax) {
    //handle if it is not a number in the TextBox
}

Method 3: TryParse()

A more advanced version which combines try-catch and Parse() is using the double.TryParse() method which returns a true/false value based on whether conversion was successful or not.

double x;
if (double.TryParse(TextBox1.Text, out x)) {
    runningSum += x;
} else {
    //handle if it is not a number in the TextBox.
}
iSpain17
  • 2,502
  • 3
  • 17
  • 26
  • 2
    It is quite a thing to write an appropriate regular expression for the *floating point number* `double` (let me provide some correct values as examples: `.1`, `-.2`, `1.23`, `1e-2`, `-1E+1`) – Dmitry Bychenko Feb 27 '19 at 10:39
  • Well, most often you don't really have to write these things yourself. You can just google them. I think the thing to take away from that method is its existence. :) See, it takes like 1 minute to find a pattern on SO: https://stackoverflow.com/questions/33939770/regex-for-decimal-number-validation-in-c-sharp – iSpain17 Feb 27 '19 at 10:42
  • 2
    *Counter examples* for an accepted answer pattern (`^-?[0-9]*\.?[0-9]+$`): `1E+1` and `-1e-1` (both *valid* but *not matched*) – Dmitry Bychenko Feb 27 '19 at 10:46
  • I wonder if anyone will ever type "1E+1" into a TextBox in 2019. But you are right, it's true! – iSpain17 Feb 27 '19 at 10:49
1

If the value needs to be a valid double you could use 'double.TryParse'. Something like:

if (double.TryParse(txtAddItem.Text, out double price) == false)
{
  // Show error or clear textbox
}
else
{
  // Value is valid, add to total
}
Eifion
  • 5,403
  • 2
  • 27
  • 27
1

You can use Double.TryParse()

double number;
if (Double.TryParse(txtAddItem.Text, out number)) {
    Console.WriteLine("'{0}' is a valid double: {1}", value, number);
} else {
    Console.WriteLine("Unable to parse '{0}' as a valid double", value);
} 
Diado
  • 2,229
  • 3
  • 18
  • 21