Solved

Money Validator

Posted on 2004-04-19
18
467 Views
Last Modified: 2008-03-06
2)  I need a new validator to capture monetary values in millions.  For example $500,000 will be entered as 0.5M.  $19,400,000 will be entered as 19.4M.  Write the validator and send me the validation expression.


This is what I am being asked by my boss, however, I'm not quite sure where to start on this one. The information is coming from a textbox. I'm not even sure if a validator is the right way to go about this. If someone could help me out, I'll love you forever.

Thanks,
Lance
0
Comment
Question by:Lance_Frisbee
  • 9
  • 7
18 Comments
 
LVL 9

Expert Comment

by:malharone
ID: 10864677
hope this helps

    Private Function getMoney(ByVal input As String) As Double
        If input Is Nothing Then
            input = ""
        End If
        input = input.Trim
        If input.Trim.Length < 1 Then
            input = "0"
        End If
        input = input.Replace(" ", "")
        input = input.Replace("$", "")
        input = input.Replace(",", "")
        input = input.ToUpper()
        If IsNumeric(input) Then    'regular format
            Return Double.Parse(input)
        Else
            Dim pos As Integer
            Dim mult As Integer = 1
            Dim dirty As Boolean = False

            Try
                Dim c As String = input.Chars(input.Length - 1)
                Dim skip As Integer = 0
                Select Case c
                    Case "M"
                        mult = 1000000
                        skip = 1
                    Case "B"
                        mult = 1000000000
                        skip = 1
                    Case "K"
                        mult = 1000
                        skip = 1
                    Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."
                        mult = 1
                End Select
                Return Double.Parse(input.Substring(0, input.Length - skip)) * mult
            Catch ex As Exception
                Throw New Exception("Invalid money value")
            End Try

            If Not dirty Then
                pos = input.IndexOf("M")
                If pos >= 0 AndAlso pos + 1 < input.Length Then
                    mult = 1000000
                End If
            End If


        End If
    End Function
0
 
LVL 2

Author Comment

by:Lance_Frisbee
ID: 10871877
Almost there... How do I hook this to my text field. My text box is "txtMoney"
0
 
LVL 9

Expert Comment

by:malharone
ID: 10871912
ok .. add a function

Private Function getMoney(byref input As TextBox) As Double
    return getMoney(input.text)
end function

so now what you can do is .. on txtMoney_lostfocus


private sub txtMoney_lostFocus( ... ) handles txtMoney.LostFocus
   try
         txtMoney.text = getMoney(txtMoney)  ' or          txtMoney.text = getMoney(txtMoney.text)
    catch ex as exception
            msgbox(ex.message)
           txtMoney.focus()
 end try
end sub
0
 
LVL 2

Author Comment

by:Lance_Frisbee
ID: 10871972
Alright, thats what i am aiming to do, is on lost focus. I understand your logic, i'm getting lost with the functions though... i.e. having two functions named getMoney? and also the txtMoney.focus() is getting an error.  Also, do i need to do anything on pageload to call these function(s)?
0
 
LVL 2

Author Comment

by:Lance_Frisbee
ID: 10871984
oh i see why the .focus() is getting an error, its because of the (...) - what should be in there?
0
 
LVL 9

Expert Comment

by:malharone
ID: 10871988
nevermind .. i didn't know this was for ASP page. the .focus() works for win-form text box. you'll have to substitute appropriate function to set focus on the textbox.
- malhar
0
 
LVL 2

Author Comment

by:Lance_Frisbee
ID: 10872018
So you aren't familiar with ASP.NET?
0
 
LVL 9

Expert Comment

by:malharone
ID: 10872021
if this is web page then "msgbox" will also not work. i'm no expert on asp stuff! so i dont know what's the approrpriate event for lost_focus. you'll then just have to do this on submit.
0
Free Trending Threat Insights Every Day

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.

 
LVL 2

Author Comment

by:Lance_Frisbee
ID: 10872063
Ok, well at least you got me started... i appreciate it... hey, i'll give you some points if you want to try and help me make this work on Submit instead :P :)
0
 
LVL 2

Author Comment

by:Lance_Frisbee
ID: 10872077
I'm just doing a dummy form, until i get this to work, as of now i just have a textbox and a submit button doing a postback. This is the code i have for the function - same as yours above.  



 Private Function getMoney(ByVal input As String) As Double
        If input Is Nothing Then
            input = ""
        End If
        input = input.Trim
        If input.Trim.Length < 1 Then
            input = "0"
        End If
        input = input.Replace(" ", "")
        input = input.Replace("$", "")
        input = input.Replace(",", "")
        input = input.ToUpper()
        If IsNumeric(input) Then    'regular format
            Return Double.Parse(input)
        Else
            Dim pos As Integer
            Dim mult As Integer = 1
            Dim dirty As Boolean = False

            Try
                Dim c As String = input.Chars(input.Length - 1)
                Dim skip As Integer = 0
                Select Case c
                    Case "M"
                        mult = 1000000
                        skip = 1
                    Case "B"
                        mult = 1000000000
                        skip = 1
                    Case "K"
                        mult = 1000
                        skip = 1
                    Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."
                        mult = 1
                End Select
                Return Double.Parse(input.Substring(0, input.Length - skip)) * mult
            Catch ex As Exception
                Throw New Exception("Invalid money value")
            End Try

            If Not dirty Then
                pos = input.IndexOf("M")
                If pos >= 0 AndAlso pos + 1 < input.Length Then
                    mult = 1000000
                End If
            End If


        End If
    End Function
0
 
LVL 9

Expert Comment

by:malharone
ID: 10872083
all you've to do is ..

on submit button click .. validate the text box instead of validating on lost_focus.
and instead of msgbox .. use alert to notify of errors.
0
 
LVL 2

Author Comment

by:Lance_Frisbee
ID: 10872105
can you show me in code, i am pretty new, so still trying to connect the dots... Like i understand all the logic, just getting it all translated is where i run into difficulty.
0
 
LVL 2

Author Comment

by:Lance_Frisbee
ID: 10873200
TO ALL:

Still looking for solution in ASP.NET to:          2)  I need a new validator to capture monetary values in millions.  For example $500,000 will be entered as 0.5M.  $19,400,000 will be entered as 19.4M.

The solution needs to convert whatever number in the textbox titled "txtMoney" to a figure in "millions (M)", and strip the commas and dollarsigns.

IDEALLY, this should be done when the textbox loses focus. If this isn't possible and it is formatted on Submit click, that is still acceptable but the lose focus method is preferred.

Thanks

Lance
0
 
LVL 9

Expert Comment

by:malharone
ID: 10873212
here's an example for posting .. you'll have to do put in the actual function names .. etc.

<form name="form1" method="post" action="textpage.asp">
  <input type="text" name="textf01">
  <input type="submit" name="Submit" value="Submit">
</form>

<%
//get parameter passed in
Dim param
param = Request.QueryString("textf01")

Sub validate(param)
//what ever function validate, say it returns boolean status
end sub

Sub afterValidate(status)
      if(status) = true then
      //do something or go some where
      else
            Response.Write "<meta http-equiv=\"refresh\" content=\"5;url=\"www.google.com\">"
      end if
end sub
%>
0
 
LVL 9

Expert Comment

by:malharone
ID: 10890814
I think the initial question was successefully answered, which was about validation. After Lance changed the requirements, I tried to help out as well. So the question should not be closed/deleted w/o points reward.
0
 
LVL 2

Author Comment

by:Lance_Frisbee
ID: 10892609
I'm not trying to be a stickler by any means... I just haven't been able to get that to work. I need a function or code that makes it so when someone enters $1,900,000 into a textbox:

[ $1,900,000 ]

When the box loses focus it's changed to

[1.9M ]

If anyone could help me with code for that. i'll increase points and reward when it's functioning.
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10911631
Closed, 80 points refunded.
modulo
Community Support Moderator
Experts Exchange
0

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

8 Experts available now in Live!

Get 1:1 Help Now