IsNumeric Function in vb.net

Babak Boloury 1 Reputation point
2021-03-24T00:27:01.937+00:00

Hi Everyone: I am using VB.NET 2019, and I have a question about the IsNumeric fundtion for checking if a textbox contains numbers or not? The code that I am using is: If Not IsNumeric(TextBox1.Text) Then MsgBox("Please enter a numeric value.", MsgBoxStyle.Critical, My.Application.Info.ProductName & " Error") TextBox1.SelectAll() TextBox1.Focus() Endif Here is my question. If in my textbox I input "5 5" (of course without the quotations), the program catches the error. However if I input "5 ", it does not catch it. Can someone please explain why, and if there are other exceptions as well. I appreciate your help. Bob

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,780 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. WayneAKing 4,926 Reputation points
    2021-03-24T01:17:05.347+00:00

    It behaves like TryParse, which accepts optional leading
    or trailing whitespace. See the Remarks here:

    https://learn.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0

    • Wayne
    1 person found this answer helpful.
    0 comments No comments

  2. Xingyu Zhao-MSFT 5,371 Reputation points
    2021-03-24T03:20:25.21+00:00

    Hi @Babak Boloury ,
    You can remove all spaces when using 'IsNumeric'.
    For example:

            If Not IsNumeric(TextBox1.Text.Replace(" ", "")) Then  
                MsgBox("Please enter a numeric value.", MsgBoxStyle.Critical, My.Application.Info.ProductName & " Error")  
                TextBox1.SelectAll()  
                TextBox1.Focus()  
            End If  
    

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Viorel 119.9K Reputation points
    2021-03-24T09:42:14.133+00:00

    It seems to also return True in these cases: "&HAC", "&o0", "5.2", "5,2", "5+". Maybe it is better to use TryParse in your case, where you can specify more parameters and even the language (culture)? Sometimes you can also use Regular Expressions.

    0 comments No comments

  4. Karen Payne MVP 35,551 Reputation points
    2021-03-24T11:34:10.233+00:00

    Here are a couple ideas

    • Use a custom TextBox (note IsNumeric assertions)
    • For a regular TextBox use either TextChanged or KeyPress events

    81110-f1.png

    The following are basics that can be expanded on

    Public Class Form1  
        Private Sub IsNumberButton_Click(sender As Object, e As EventArgs) Handles IsNumberButton.Click  
      
            If SpecialTextBox1.IsNumeric Then  
                MessageBox.Show("Numeric")  
            Else  
                MessageBox.Show("Not numeric")  
            End If  
      
        End Sub  
        ''' <summary>  
        ''' Integer, Decimal, Double  
        ''' </summary>  
        ''' <param name="sender"></param>  
        ''' <param name="e"></param>  
        Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress  
      
            If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) AndAlso (e.KeyChar <> "."c) Then  
                e.Handled = True  
            End If  
      
            If (e.KeyChar = "."c) AndAlso ((TryCast(sender, TextBox)).Text.IndexOf("."c) > -1) Then  
                e.Handled = True  
            End If  
      
        End Sub  
        ''' <summary>  
        ''' Integer only  
        ''' </summary>  
        ''' <param name="sender"></param>  
        ''' <param name="e"></param>  
        Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged  
              
            Dim textBox = ctype(sender, TextBox)  
      
            If System.Text.RegularExpressions.Regex.IsMatch(TextBox.Text, "[^0-9]") Then  
                TextBox.Text = TextBox2.Text.Remove(TextBox.Text.Length - 1)  
            End If  
      
        End Sub  
    End Class  
      
    Public Class SpecialTextBox  
        Inherits TextBox  
      
        ''' <summary>  
        ''' Determine if we are dealing with a number  
        ''' </summary>  
        ''' <returns></returns>  
        Public ReadOnly Property IsNumeric() As Boolean  
            Get  
                If Double.TryParse(Text.Replace(" "c, "").Trim(), Nothing) Then  
                    Return True  
                Else  
                    Return False  
                End If  
            End Get  
        End Property  
      
    End Class  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.