Hello all,

I'm trying to validate against someone entering letters into an input box. Here is the code I have so far that doesn't seem to do work after I add the second part to verify if what they entered is numeric. I don't even the get input box anymore. As soon as I take it out, it works fine by poping up the box each time a 0 or a negative number is entered.

While carpetArea <= 0 And Not IsNumeric(CStr(carpetArea))
                carpetArea = InputBox("Please enter the area that you want the carpet installed in:", "Area", "0")
            End While

I'm converting carpetArea from a double to a string so I can use it with isNumeric. Am I doing it wrong or is that somehow messing things up? Is there perhaps a better way? I have to use a input box so I can't don't have the option to do a text box.

Thanks.

Handle the KeyDown and KeyPress events of a textbox.
Check if every key pressed is numeric.

Unfortunately I have to use a text box for all input.

I think since you "can't" use textbox and have to use input box then your best bet is to switch it to a string and then try to parse the input. If you can then it's numeric and you can go with it but if it can't be parsed then it's not numeric.

Why are you conveting to string when you do the IsNumeric() check?

As far as i know inputbox() returns a string even if the user only types in numbers. So instead of converting carpetarea into a string use Csng()

Thanks for the suggestion, I went ahead and tried it .

While carpetArea <= 0 And Not IsNumeric(CSng(carpetArea))

But it ended up doing the same thing...I click on a carpet selection in the list box and then click install and don't get a input box prompting for area anymore.

What exactly does CSng do?

Csng() converts string or any other data type into a single.

The problem with what you did was that the only way to acces the loop is if the carpetarea is numeric and is less or equal to 0 however, you do not put a number into carpetarea before you do that check.

To fix your problem do this:

carpetArea = InputBox("Please enter the area that you want the carpet installed in:", "Area", "0")

            While carpetArea <= 0 And Not IsNumeric(CSng(carpetArea))
                carpetArea = InputBox("Please enter the area that you want the carpet installed in:", "Area", "0")
            End While

Makes sense but now when I try it, the input box does come up, but if its a negative number or 0 it just accepts it.

Also, if it's a letter it still crashes with an invalid cast exception.

Can you show me the code you have so far and when do you want the loop to stop?

kk Found the problem instead of IsNumeric(CSng(carpetArea)) just put IsNumeric(carpetArea).

Sure...I'd like the loop to stop when its a positive number.

Negative numbers, 0 and letters entered should keep looping the input box to keep coming up until the cows come home.

Here's all of my relevant code.

Private Sub rdoCarpetQuote_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdoCarpetQuote.CheckedChanged

        While lstCarpets.SelectedIndex = -1 And rdoCarpetQuote.Checked = True
            rdoCarpetQuote.Checked = False
        End While

        If rdoCarpetQuote.Checked = True Then
            grpPrice.Visible = True

            carpetArea = InputBox("Please enter the area that you want the carpet installed in:", "Area", "0")

            While carpetArea <= 0 And Not IsNumeric(CSng(carpetArea))

                carpetArea = InputBox("Please enter the area that you want the carpet installed in:", "Area", "0")
            End While

            carpetSub = carpetArea * carpetUnit
            lblCarpetPrice.Text = carpetSub.ToString("c")
            carpetLaborSub = carpetArea * laborRate
            lblLaborPrice.Text = carpetLaborSub.ToString("c")
            carpetSubTotal = carpetSub + carpetLaborSub
            lblTotal.Text = carpetSubTotal.ToString("c")
            grpPadding.Visible = True
        End If
    End Sub

Ok I tried same code as above except with

carpetArea = InputBox("Please enter the area that you want the carpet installed in:", "Area", "0")

            While carpetArea <= 0 And Not IsNumeric(carpetArea)

                carpetArea = InputBox("Please enter the area that you want the carpet installed in:", "Area", "0")
            End While

and it still accepted negative numbers, 0 and crashed with letters.

Just in case this was it, I went to the beginning of the program and found carpetArea declared as a double instead of a single so I changed the while condition to this

While carpetArea <= 0 And Not IsNumeric(CDbl(carpetArea))

with the same results.

I still think you should use tryparse to keep from crashing on letters, you would have to add a variable to hold the parsed double

While Double.TryParse(carpetArea, dblNumber) And carpetArea > 0.0

this should work, if the number is not able to be converted to double it will throw warning and if it's negative or zero it will not hit your while.

Surroud it with a try catch block

It's working!

Thanks a lot.

I'm using this code:

Try
                While carpetArea <= 0 And Double.TryParse(carpetArea, dblNumber)
                    carpetArea = InputBox("Please enter the area that you want the carpet installed in:", "Area", "0")
                End While

            Catch
                MessageBox.Show("ERROR:Please enter only numbers")
                carpetArea = InputBox("Please enter the area that you want the carpet installed in:", "Area", "0")
            End Try

Do you think it is good enough to tell them about the error and then just present them with another input box again?

Yes, that's fine. You might add the word positive to it but hey that's just preference.

If it's solved, would you please mark the thread as solved?

Sure thx a lot to everyone who tried to help out as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.