Solved

Check if number is currency

Posted on 2016-11-16
15
27 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 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Please provide some sample data to test :)
0
 

Author Comment

by:RIAS
Comment Utility
123+ or 123,
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
So what we need as output for these ?
0
 

Author Comment

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

Author Comment

by:RIAS
Comment Utility
isnumeric does not filter
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
What about 123. ?

Valid or Invalid ?
0
 

Author Comment

by:RIAS
Comment Utility
Valid,
Any currency format is valid
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 16

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 100 total points
Comment Utility
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 32

Accepted Solution

by:
it_saige earned 400 total points
Comment Utility
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
Comment Utility
Thanks Sir!
0
 
LVL 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Hi,

my solution not worked :)

regards,
Pawan
0
 

Author Comment

by:RIAS
Comment Utility
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 16

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Thanks bro..
0
 
LVL 32

Assisted Solution

by:it_saige
it_saige earned 400 total points
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now