简体   繁体   中英

How do I validate user input?

So I'm having a problem with my code. I need to validate if the user input is a number or not in a textbox. Now I can get it to see whether or not it is a number and it displays the error message just fine but the problem is that the word still gets inputted in to the textbox when I want there to be only numbers

If tried using if not IsNumeric(Number) then msgbox.show("ERROR! Data must be a number!")

  'Getting user input 
    Dim Number As String = Me.InputTextbox.Text
    UnitsTextbox.AppendText(Environment.NewLine & Number)

    'Make the textbox delete the text once the button is clicked
    InputTextbox.Text = String.Empty

    If Not IsNumeric(Number) Then
        MsgBox("ERROR! Data must be a number")
    End If

I'm expecting it to accept numbers only

i have a text box for input and a textbox for the results and when the number comes up false I want it to not show in the results textbox

As per @Jens comments, only I changed it to .TryParse. IsNumeric is an old VB6 method that has been optimized in .Net to TryParse.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim output As Integer
    If Not Integer.TryParse(InputTextbox.Text, output) Then
        MessageBox.Show("ERROR! Data must be a number")
    Else
        UnitsTextbox.AppendText(Environment.NewLine & InputTextbox.Text)
        InputTextbox.Text = String.Empty
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM