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:
Public Function IsNumeric(ByVal inputString As String) As Boolean
Dim _isNumber As System.Text.RegularExpressions.Regex = New _
System.Text.RegularExpressions.Regex("(^[-+]?\d+(,?\d*)*\.?\d*([Ee][-+]\d*)?$)|(^[-+]?\d?(,?\d*)*\.\d+([Ee][-+]\d*)?$)")
Return _isNumber.Match(inputString).Success
End Function
We can call this function like this
VB Code:
If isNumeric("1234") Then
MessageBox.Show("String is Numeric")
Else
MessageBox.Show("String is not Numeric")
End If
Edit--
I have got this RegEx from Internet which checks almost all the possibilities.
Re: VB.NET: IsNumeric using RegEX
Thought I might post this bit of code in the same type thread
VB Code:
Private Function IsNumeric(ByVal Str As String) As Boolean
Const Upper As Int32 = 57
Const Lower As Int32 = 48
For Each a As Char In Str
Dim Base As Int16 = Convert.ToInt16(a)
If (Base <= Upper And Base >= Lower) = False Then
Return False
End If
Next
Return True
End Function