简体   繁体   English

如何验证用户输入?

[英]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!") 如果尝试使用非IsNumeric(Number),则msgbox.show(“ ERROR!数据必须为数字!”)

  '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 我有一个用于输入的文本框和一个用于结果的文本框,当数字为false时,我希望它不显示在结果文本框中

As per @Jens comments, only I changed it to .TryParse. 根据@Jens注释,只有我将其更改为.TryParse。 IsNumeric is an old VB6 method that has been optimized in .Net to TryParse. IsNumeric是一种旧的VB6方法,已在.Net中优化为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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