Solved

Check if number is currency

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

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 29

Expert Comment

by:Pawan Kumar
ID: 41889385
So what we need as output for these ?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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 29

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 29

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 34

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 29

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 29

Expert Comment

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

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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

627 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