Results 1 to 2 of 2

Thread: VB.NET: IsNumeric using RegEX

  1. #1

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Lightbulb VB.NET: IsNumeric using RegEX

    To check if a string is a valid number we either have to use the Try..Catch..Finally with Integer.Parse or try Double.TryParse.

    I just saw a method using regular expressions sometime back
    VB Code:
    1. Public Function IsNumeric(ByVal inputString As String) As Boolean
    2.         Dim _isNumber As System.Text.RegularExpressions.Regex = New _
    3. System.Text.RegularExpressions.Regex("(^[-+]?\d+(,?\d*)*\.?\d*([Ee][-+]\d*)?$)|(^[-+]?\d?(,?\d*)*\.\d+([Ee][-+]\d*)?$)")
    4.         Return _isNumber.Match(inputString).Success
    5.     End Function
    We can call this function like this
    VB Code:
    1. If isNumeric("1234") Then
    2.    MessageBox.Show("String is Numeric")
    3. Else
    4.    MessageBox.Show("String is not Numeric")
    5. End If

    Edit--
    I have got this RegEx from Internet which checks almost all the possibilities.
    Last edited by Shuja Ali; Mar 23rd, 2006 at 06:48 AM. Reason: Changed the RegEX to include possible numeric combinations
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  2. #2
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: VB.NET: IsNumeric using RegEX

    Thought I might post this bit of code in the same type thread


    VB Code:
    1. Private Function IsNumeric(ByVal Str As String) As Boolean
    2.         Const Upper As Int32 = 57
    3.         Const Lower As Int32 = 48
    4.         For Each a As Char In Str
    5.             Dim Base As Int16 = Convert.ToInt16(a)
    6.             If (Base <= Upper And Base >= Lower) = False Then
    7.                 Return False
    8.             End If
    9.         Next
    10.         Return True
    11.     End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width