Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Check if number is currency

Posted on 2016-11-16
15
Medium Priority
?
48 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
15 Comments
 
LVL 30

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 30

Expert Comment

by:Pawan Kumar
ID: 41889385
So what we need as output for these ?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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 30

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 30

Assisted Solution

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

Open in new window

0
 
LVL 34

Accepted Solution

by:
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
		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 30

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 30

Expert Comment

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

Assisted Solution

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

Open in new window

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

Featured Post

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

670 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