Solved

# Check if number is currency

Posted on 2016-11-16
Medium Priority
54 Views
Hello,
If IsNumeric(tb.Text) = False Then

works but does accepts
123+ or 123,

How can I inform the user that it has invalid characters

Cheers
0
Question by:RIAS
• 6
• 6
• 2

LVL 38

Expert Comment

ID: 41889381
Please provide some sample data to test :)
0

Author Comment

ID: 41889382
123+ or 123,
0

LVL 38

Expert Comment

ID: 41889385
So what we need as output for these ?
0

Author Comment

ID: 41889389
Just a message saying + or , is invalid
must be numbers
0

Author Comment

ID: 41889390
isnumeric does not filter
0

LVL 38

Expert Comment

ID: 41889395

Valid or Invalid ?
0

Author Comment

ID: 41889399
Valid,
Any currency format is valid
0

LVL 38

Assisted Solution

Pawan Kumar earned 400 total points
ID: 41889401
Try this ..

``````   Dim regex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("^[0-9]\d*(\.\d{0,9})?%?\$")
Dim match As System.Text.RegularExpressions.Match = regex.Match("0.9")

Dim a As String

If match.Success Then
a = "CURRENCY"
Else
a = "NON CURRENCY"
End If

Console.WriteLine(a)
``````
0

LVL 36

Accepted Solution

it_saige earned 1600 total points
ID: 41889655
You could also simply try to parse the text as a double value; e.g. -
``````Module Module1
Sub Main()
Dim values = {"123+", "123", "123."}
Dim dblOut = 0.0
For Each value In values
If Double.TryParse(value, dblOut) Then
Console.WriteLine("{0} is a valid double value; Currency format - {1:C2}", value, dblOut)
Else
Console.WriteLine("{0} is not a valid double value", value)
End If
Next
End Sub
End Module
``````
Produces the following output -
-saige-
0

Author Comment

ID: 41889697
Thanks Sir!
0

LVL 38

Expert Comment

ID: 41889701
Hi,

my solution not worked :)

regards,
Pawan
0

Author Comment

ID: 41889719
Oh Pawan,

My phone could not load it.
Will try it as well and get back.
If it works will request moderator to split points.

Cheers
0

LVL 38

Expert Comment

ID: 41889729
Thanks bro..
0

LVL 36

Assisted Solution

it_saige earned 1600 total points
ID: 41889740
I see no reason why Pawan's recommendation shouldn't work, but you have to remember to convert the matched value into a double in order to apply the currency format.  Proof of concept:
``````Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim values = {"123+", "123", "123."}
Dim expression As New Regex("^[0-9]\d*(\.\d{0,9})?%?\$")
Dim dblOut = 0.0
For Each value In values
If Double.TryParse(value, dblOut) Then
Console.WriteLine("{0} is a valid double value; Currency format - {1:C2}", value, dblOut)
Else
Console.WriteLine("{0} is not a valid double value", value)
End If

Console.Write("Using Pawan's solution -")
Dim match = expression.Match(value)
If match.Success Then
Console.WriteLine("{0} is a valid double value; Currency format - {1:C2}", value, Convert.ToDouble(match.Value))
Else
Console.WriteLine("{0} is not a valid double value", value)
End If

Console.WriteLine()
Next
End Sub
End Module
``````
Produces the following output -
-saige-
1

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.