Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Check if Field is numeric

Posted on 2006-05-05
3
Medium Priority
?
340 Views
Last Modified: 2012-06-27
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
Comment
Question by:Michael Krumpe
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
jrscherer earned 2000 total points
ID: 16618180

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

Jack.net

0
 
LVL 7

Expert Comment

by:wtconway
ID: 16618328
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
 
LVL 7

Expert Comment

by:wtconway
ID: 16618349
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question