Solved

Check if number is currency

Posted on 2016-11-16
15
37 Views
Last Modified: 2016-11-16
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
Comment
Question by:RIAS
  • 6
  • 6
  • 2
15 Comments
 
LVL 28

Expert Comment

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

Author Comment

by:RIAS
ID: 41889382
123+ or 123,
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41889385
So what we need as output for these ?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

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

Author Comment

by:RIAS
ID: 41889390
isnumeric does not filter
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41889395
What about 123. ?

Valid or Invalid ?
0
 

Author Comment

by:RIAS
ID: 41889399
Valid,
Any currency format is valid
0
 
LVL 28

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 100 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)

Open in new window

0
 
LVL 33

Accepted Solution

by:
it_saige earned 400 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
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -Capture.JPG
-saige-
0
 

Author Comment

by:RIAS
ID: 41889697
Thanks Sir!
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41889701
Hi,

my solution not worked :)

regards,
Pawan
0
 

Author Comment

by:RIAS
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 28

Expert Comment

by:Pawan Kumar
ID: 41889729
Thanks bro..
0
 
LVL 33

Assisted Solution

by:it_saige
it_saige earned 400 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
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -Capture.JPG
-saige-
1

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question