looking for isnumeric in vb.net?
i was wondering why a vb.net class file was throwing up errors when i called the isnumeric function the other day. after some googling i found out from roy osherove’s blog that you now need to import the microsoft.visualbasic namespace to use this and a bunch of other once standard functions.
as an aside, if you want to pause your program and are wondering why sleep() does not seem to work, you now need to reference system.threading.thread.sleep() instead.



February 11th, 2006 at 12:13 pm
This little snippet will detect if an input is a numeric value.
Public Function IsNumeric(ByVal str As String)
Dim r As Regex = New Regex(”\d+”)
Dim m As Match = r.Match(str)
If (m.Success) Then
Return True
End If
Return False
End Function
If Not (IsNumeric(TextBox4.Text)) Then
MessageBox.Show(”A listening port must be a numeric value”, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
February 12th, 2006 at 12:27 pm
thanks for the contribution peter - regular expressions to the rescue once again
November 29th, 2006 at 11:19 am
Or even simpler:
Imports System.Text.RegularExpressions
Public Shared Function IsNumeric(ByVal str As String)
Return New Regex(”\d+”).Match(str).Success
End Function
November 29th, 2006 at 11:20 am
oops, make that:
Public Shared Function IsNumeric(ByVal str As String) as Boolean
Return New Regex(”\d+”).Match(str).Success
End Function
November 29th, 2006 at 7:25 pm
Unfortunately that regex expression doesn’t work in every case. I am parsing a file and I am getting some non-standard characters at the end of a number because of some bad data that was sent as the power failed. This expression still returns true and my program fails as it tries to convert the number with the bad characters….
March 6th, 2007 at 4:10 pm
The proper regex expression should be: ^\d+$
This will verify that the start of the value is numeric, not just search for any number in the supplied value
June 24th, 2007 at 1:22 am
I’ve had the best luck with “\b\d+(\.\d+)?\b” as my regular expression pattern since it will match doubles and floats as well.
My variation on Kevin’s function would be:
Public Shared Function isNumeric(ByVal textToCheck as String) as Boolean
return Regex.IsMatch(textToCheck, “\b\d+(\.\d+)?\b”)
End Function
Just a bit cleaner in my humble opinion.
August 23rd, 2007 at 6:00 pm
I get nothing but false positives with the functions listed above
I created my own and is perfect for detecting ‘all numeric’ values…great for detecting table indexes (just make sure you trim the text first):
Public Shared Function IsNumeric(ByVal sText As String) As Boolean
Dim b As Boolean = True
Dim x As Integer = 0
For x = 0 To sText.Length - 1
Dim sChar As String = sText.Substring(x, 1)
If Asc(sChar) 57 Then
b = False
Exit For
End If
Next
Return b
End Function
August 23rd, 2007 at 6:01 pm
ok..that didnt paste well…removed some of my code..trying again
Public Shared Function IsNumeric(ByVal sText As String) As Boolean
Dim b As Boolean = True
Dim x As Integer = 0
For x = 0 To sText.Length - 1
Dim sChar As String = sText.Substring(x, 1)
If Asc(sChar) < 48 Or Asc(sChar) > Then
b = False
Exit For
End If
Next
Return b
End Function
September 9th, 2007 at 1:44 am
This seems to work well for me.
Public Shared Function IsNumeric(ByVal sText As String) As Boolean
If Double.TryParse(sText, Globalization.NumberStyles.AllowDecimalPoint) Then
Return True
Else
Return False
End If
End Function
May 8th, 2008 at 3:11 pm
Why reinvent the wheel? IsNumeric is part of .NET. Just use it.