Results 1 to 2 of 2

Thread: VB.NET: IsNumeric using RegEX

Threaded View

  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

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