Solved

Check if number is currency

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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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