Function to Validate Double Value with or without Currency Symbol - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This is a tutorial on how to validate a double value, or a currency value in Visual Basic .NET

validate double in visual basic .net

Add the following function to validate the user input

Function validateDoublesAndCurrency(stringValue As String) As Boolean
        Dim rslt As Boolean = False
        Dim value As Double
        Dim valueToTest As String = stringValue
        Try
            'check if value to be tested contains a currency symbol such as a dollar sign ($)
            valueToTest = Double.Parse(stringValue, Globalization.NumberStyles.Currency)
        Catch ex As Exception

        End Try
        'check if double
        If Double.TryParse(valueToTest, value) Then
            'value is double
            rslt = True
        Else
            'value is not double
            rslt = False
        End If
        Return rslt
    End Function

The add the following button click event code

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If validateDoublesAndCurrency(TextBox1.Text) = False Then
            MsgBox("Price entered is not valid")
        Else
            MsgBox("Price is Valid")
        End If

    End Sub

Value to TestResult
$1.99True
1.99True
1.9True
1True
1,999.99True
0.99True
$1.99AFalse
ABCFalse
1-99False


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.