Validating the data in a tip calculator in C#

1808 views c#

I am trying to get where when you don't enter a number or a letter in the box you get a pop-up box that said Please enter your number.

//converted a textbox into a decimal
Decimal enterNumber = Convert.ToDecimal(txtUserInput.Text);
// as well as vaidate the data
if (enterNumber<=0) {
    MessageBox.Show("Please enter your number");

answered question

1 Answer


You can check your input with txtUserInput.Text.Length > 0 && (txtUserInput.Text.All(c => char.IsDigit(c)) || (txtUserInput.Text[0] == '-' && (txtUserInput.Text.Skip(1).All(c => char.IsDigit(c))))

Returns True if the string is a valid number.

posted this

Have an answer?


Please login first before posting an answer.