【问题标题】:Validate text box input for numeric range 1-100验证数字范围 1-100 的文本框输入
【发布时间】:2017-01-24 20:51:43
【问题描述】:

我正在尝试验证输入以仅接受 1-100 的数字范围。我有它,它将接受 1-100 范围内的数字。我需要的是如何确保不能使用诸如字母或符号(?、/、 等)之类的字符输入。我正在使用循环来查看放入数组中的每个输入。验证输入后,我将返回一个 blnOk = True 以在我的按钮事件中使用以运行程序。我试过 double.TryParse 但我无法让它与数组一起工作。我也有 Option Strict ON。 dblStudentTestScores(i) 是存储输入的数组。txtTestScores(i) 数组用于告诉程序循环在哪个文本框上,以便在出错时获得焦点。

Private Sub ValidateScores()

    For i = 0 To 8
        If dblStudentTestScores(i) >= 0 And dblStudentTestScores(i) <= 100 Then
            blnOK = True

        Else
            MessageBox.Show("Please Enter Test Score between 0 and 100")
            txtTestScores(i).Clear()
            txtTestScores(i).Focus()
            txtTestScores(i).BackColor = Color.Yellow
            blnOK = False
            Exit Sub
        End If
        txtTestScores(i).BackColor = Color.White
    Next
    blnOK = True
End Sub

这是带有 IsNumeric 的代码,但它不会进入 Else 子句。它跳入try catch。我真的希望它能够专注于具有无效输入的文本框。就像数字不在 0 到 100 之间一样。

 Private Sub ValidateScores()

    For i = 0 To 8
        If CInt(IsNumeric(dblStudentTestScores(i))) >= 0 And CInt(IsNumeric(dblStudentTestScores(i))) <= 100 Then
            blnOK = True

        Else
            MessageBox.Show("Please Enter Test Score between 0 and 100")
            txtTestScores(i).Clear()
            txtTestScores(i).Focus()
            txtTestScores(i).BackColor = Color.Yellow
            blnOK = False
            Exit Sub
        End If
        txtTestScores(i).BackColor = Color.White
    Next
    blnOK = True
End Sub

每个输入都填充数组。

Public Sub PopulateTestScores()
    'Student 1 test scores
    dblStudentTestScores(0) = CDbl(txtStudent1Score1.Text)
    dblStudentTestScores(1) = CDbl(txtStudent1Score2.Text)
    dblStudentTestScores(2) = CDbl(txtStudent1Score3.Text)
    'Student 2 test scores
    dblStudentTestScores(3) = CDbl(txtStudent2Score1.Text)
    dblStudentTestScores(4) = CDbl(txtStudent2Score2.Text)
    dblStudentTestScores(5) = CDbl(txtStudent2Score3.Text)
    'Student 3 test scores
    dblStudentTestScores(6) = CDbl(txtStudent3Score1.Text)
    dblStudentTestScores(7) = CDbl(txtStudent3Score2.Text)
    dblStudentTestScores(8) = CDbl(txtStudent3Score3.Text)
End Sub

【问题讨论】:

  • 这段代码有什么问题?这是验证测试,但我看不到你在哪里输入并检查它们是否真的是数字。
  • 它仍然允许我输入诸如 ?,/,>,' 之类的字符,甚至允许输入字母。我只需要被允许在输入中使用数字。在这一点上,我唯一要做的验证就是检查输入是否在 0 到 100 之间。
  • 您应该使用 double.TryParse 来检查输入是否有效。你能展示将双打添加到数组中的代码吗?
  • 另外,你说你有 Option Strict On 所以我假设你的数组 dblStudentTestScores 是一个双数组。对吗?
  • 输入是双精度的。

标签: arrays vb.net visual-studio validation


【解决方案1】:

您应该使用double.TryParse,因为如果输入不是有效的双精度,它会返回 false 而不是抛出异常。

Public Function PopulateTestScores() as Boolean
    Dim ok as Boolean
    ok = CheckInput(txtStudent1Score1.Text, 0)
    if not ok Then  
        txtStudent1Score1.Focus()
        return ok
    End if

    ok = CheckInput(txtStudent1Score2.Text, 1)
    if not ok Then  
        txtStudent1Score2.Focus()
        return ok
    End if
    .....
    return ok
End Function

private Function CheckInput(input as String, index as integer) as Boolean
    Dim d as Double        
    Dim ok as Boolean
    ok = double.TryParse(input, d)
    if ok Then dblStudentTestScores(index) = d
    return ok
End Function

带有两个参数的 TryParse 版本要求字符串采用与您的语言环境相同的格式(点/逗号表示小数)。如果您想使用不同的文化,那么只需使用适当的 TryParse 重载,该重载也采用 CultureInfo 参数。

例如,如果您使用采用 NumberStyles 和 CultureInfo 的重载

ok = Double.TryParse(input, NumberStyles.None, CultureInfo.CurrentCulture, d)

如果输入与您的 CultureInfo 和数字分组的正确约定不匹配,TryParse 将返回 false。

在 en-US 文化中像 10.1 这样的有效双精度在正确输入为 10,1it-IT 中是无效的

【讨论】:

    【解决方案2】:

    您可以使用isNumeric 函数检查输入。

    https://msdn.microsoft.com/en-us/library/6cd3f6w1(v=vs.90).aspx

    【讨论】:

    • 当我使用 IsNumeric 时,它会跳到 Try/Catch。我需要它跳转到 Else 子句并将消息和焦点放在该文本框上。 IsNumeric 确实停止获取字符和字母。有没有办法强制它进入Else?
    • 我对 Visual Basic 不太熟悉。我非常生疏,但在大多数语言中,您可以使用! 表示不表示,基本上是说如果它不是数字,那么就这样做。所以if !isNumeric()。然后只需将else 子句中的代码切换到if 子句,反之亦然
    猜你喜欢
    • 2013-12-25
    • 2022-06-13
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    相关资源
    最近更新 更多