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.
October 29th, 2008 at 11:34 pm
oops. here’s the correct one.
public function IsNumeric(ByVal textz as string) as boolean
dim ch as boolean
for each c as byte in System.Text.Encoding.ASCII.getbytes(text)
if (48 <= c <= 57) then
c2 = true ‘good job.
else
c2 = false ‘bad job ):
return false
exit function
end if
next
return c2
end function
October 31st, 2008 at 12:02 am
in the keypress event of the textbox, you could add
Dim mychar As Char
mychar = e.KeyChar
If IsNumerical(mychar.ToString) = True Then
‘Insert positive code here
else
msgbox(“Dammit I said numbers only”)
This checks each character as its passed to the txtbox,
January 31st, 2009 at 11:55 pm
Integer.TryParse(MyText, MyDestNumber)
OR
if Integer.TryParse(MyText, MyDestNumber) then …
if you need decimal values then
Double.TryParse(MyText, MyDestNumber)
OR
if Double.TryParse(MyText, MyDestNumber) then …
February 4th, 2009 at 7:25 am
Use this to the textbox KeyPress event, it accept only number and maximum one “.” and ignore others without add into it:
Private Sub txtMyTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSSV.KeyPress
Select Case e.KeyChar
Case Chr(Keys.Back)
e.Handled = False
Case “0″, “1″, “2″, “3″, “4″, “5″, “6″, “7″, “8″, “9″, “.”
Try
Dim strOutput As Double = 0.0
If Double.TryParse(txtSSV.Text & e.KeyChar, strOutput) Then
e.Handled = False
Else
e.Handled = True
End If
Catch ex As Exception
e.Handled = True
End Try
Case Else
e.Handled = True
End Select
End Sub
June 23rd, 2009 at 4:12 pm
Why not just use the microsoft.visualbasic namespace version of isnumeric? So much easier that all this that you’ve posted.
December 2nd, 2009 at 4:17 pm
As with all VB coding, you do NOT have to import a Namspace to in order to use it. Just call that namespace.function:
Microsoft.VisualBasic.IsNumeric(expression)
Besides, as Carter says .. if you’re programming in the .NET framework in VB (which if you are programming in VS2003+, then you have no choice), just inherit/import the Microsoft/System namespaces .. You have to the .NET framework installed on the computer anyways to run the program, so you don’t have to worry about NOT having a particular DLL