Solved

Money Validator

Posted on 2004-04-19
18
479 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
[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
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

623 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