-1

I need to validate a certain textbox input to only accept numerical data between the range of 0-10 and so that is shows an error message in a label when the button is pushed and the data isn't within those specifications. This then clears the textbox, focuses the textbox and exits the sub routine if the data is invalid.

Current code is:

Select Case decScore1
        Case txtScore1.Text = ""
            strScore1Error = "Score 1 is blank"
            lblStatusShow.Text = strScore1Error
            txtScore1.Clear()
            txtScore1.Focus()
            Exit Sub

        Case Not IsNumeric(txtScore1.Text)
            strScore1Error = "Score 1 is not a numeric value: 0-10"
            lblStatusShow.Text = strScore1Error
            txtScore1.Clear()
            txtScore1.Focus()
            Exit Sub

        Case decScore1 > 10 Or decScore1 < 0
            strScore1Error = "Score 1 is not in the range: 0-10"
            lblStatusShow.Text = strScore1Error
            txtScore1.Clear()
            txtScore1.Focus()
            Exit Sub

        Case Else
            strScore1Error = "Score 1 input is valid"
    End Select`

The issues are:

If txtScore1 is left blank, lblStatusShow = "Score 1 is not in the range: 0-10" when I need it to have the text "Score 1 is blank"

If txtScore1 is not a numeric value, lblStatusShow = "Score 1 is blank"

If txtScore1 is outside of the range 0-10, lblStatusShow = "Score 1 is valid"

So essentially it's not validating in the right way at all and I'm not sure why or how to fix it.

2 Answers2

0

You are telling the compiler to take the value of deScore1 in the Select Case and then checking the value of txtScore11.Text.

I would suggest this

Dim score As Integer    
With txtScore1
    If IsNumeric(.Text) Then
        strScore1Error = "Score 1 is not a numeric value: 0-10"
        lblStatusShow.Text = strScore1Error
        txtScore1.Clear()
        txtScore1.Focus()
        Exit Sub
    ElseIf .Text.Trim.Length = 0 Then
        strScore1Error = "Score 1 is blank"
        lblStatusShow.Text = strScore1Error
        txtScore1.Clear()
        txtScore1.Focus()
        Exit Sub
    ElseIf Integer.TryParse(.Text, score) Then 
        If (score > 10 or score < 0 Then
            strScore1Error = "Score 1 is not in the range: 0-10"
            lblStatusShow.Text = strScore1Error
            txtScore1.Clear()
            txtScore1.Focus()
            Exit Sub
        End If
    Else
        strScore1Error = "Score 1 input is valid"
    End If
End With
Saagar Elias Jacky
  • 2,684
  • 2
  • 14
  • 28
0
  Select Case decScore1

            Case 1 To 10
                strScore1Error = "Score "
                lblStatusShow.Text = strScore1Error

            Case Nothing
                strScore1Error = "Blank "
                lblStatusShow.Text = strScore1Error

            Case Is > 10
                strScore1Error = "Score is more "
                lblStatusShow.Text = strScore1Error


            Case Else
                strScore1Error = "Score 1 is not a numeric value: 0-10"
                lblStatusShow.Text = strScore1Error
        End Select
Tharif
  • 13,794
  • 9
  • 55
  • 77