Solved

Money Validator

Posted on 2004-04-19
18
473 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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
 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
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 …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

778 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