• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

Check if Field is numeric

I have a text field on form, runat=server, that when the user finishes typing it it, I want to validate that it is numeric only in the code behind.

 Private Sub txt_buildid_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_buildid.TextChanged
        If Me.txt_buildid.Text.Trim.GetType =        ?
        End If
    End Sub
0
Michael Krumpe
Asked:
Michael Krumpe
  • 2
1 Solution
 
jrschererCEOCommented:

   If IsNumeric(Me.txt_buildid.Text.Trim) Then .....

Jack.net

0
 
wtconwayCommented:
The IsNumeric function will do for simple checks. However, if you would like to filter the value as the user types it in you might want to try something slightly different. Consider, for example: (txt is a TextBox on my form).

30,500.05 <- this is a number. The IsNumeric function will accept it. CType("30,500.05",Double) will process correctly. But what if you wanted to filter the data as the user typed this in with something like this:

    Private Sub txt_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt.KeyUp
        txt.Text = FilterNumber(txt.Text, ",.")
    End Sub

    Friend Shared Function FilterNumber(ByVal p_Value As String, ByVal p_SpecialChars As String) As String
        Dim ret As String
        Dim i As Integer
        For i = 1 To p_Value.Length
            If IsNumeric(p_Value.Chars(i - 1)) Then 'NOTICE I'M USING ISNUMERIC()
                ret &= p_Value.Chars(i - 1)
            End If
        Next
        Return ret
    End Function

This will reject any non-numeric characters as they are typed. But if you wanted to allow the user the ability to provide special characters like commas and decimals you would want to change it up a bit. Like this:

    Private Sub txt_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt.KeyUp
        txt.Text = FilterNumber(txt.Text)
    End Sub

    Friend Shared Function FilterNumber(ByVal p_Value As String) As String
        Dim ret As String
        Dim i As Integer
        For i = 1 To p_Value.Length
            If IsNumber(p_Value.Chars(i - 1)) Then 'NOTICE I'M USING ISNUMERIC()
                ret &= p_Value.Chars(i - 1)
            End If
        Next
        Return ret
    End Function

    Friend Shared Function IsNumber(ByVal p_Char As Char, ByVal p_SpecialChars As String) As Boolean
        Dim ret As Boolean = False 'assume it's not a number value
        Select Case p_Char
            Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"
                ret = True
            Case Else
                If p_SpecialChars.IndexOf(p_Char) >= 0 Then
                    ret = True
                End If
        End Select
        Return ret
    End Function

This will go ahead and prevent the user from typing any character that is not a 0-9 or any of the special characters like comma, period, you could add any character you like. It's pretty easy to understand and ridiculously easy to implement for added error prevention.
0
 
wtconwayCommented:
I totally just messed that up. I meant to type the last functions first and the first functions last. Point is: give this a shot on your form and see what happens:

    Private Sub txt_buildid_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt_buildid.KeyUp
        txt_buildid.Text = FilterNumber(txt_buildid.Text, ",.")
    End Sub

    Friend Shared Function FilterNumber(ByVal p_Value As String, ByVal p_SpecialChars As String) As String
        Dim ret As String
        Dim i As Integer
        For i = 1 To p_Value.Length
            If IsNumeric(p_Value.Chars(i - 1)) Then 'NOTICE I'M USING ISNUMERIC()
                ret &= p_Value.Chars(i - 1)
            End If
        Next
        Return ret
    End Function

    Friend Shared Function IsNumber(ByVal p_Char As Char, ByVal p_SpecialChars As String) As Boolean
        Dim ret As Boolean = False 'assume it's not a number value
        Select Case p_Char
            Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"
                ret = True
            Case Else
                If p_SpecialChars.IndexOf(p_Char) >= 0 Then
                    ret = True
                End If
        End Select
        Return ret
    End Function

I wish I could delete comments from a thread.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now