# Money Validator

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
LVL 2
###### Who is Participating?

Commented:
Closed, 80 points refunded.
modulo
Community Support Moderator
Experts Exchange
0

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

Author Commented:
Almost there... How do I hook this to my text field. My text box is "txtMoney"
0

Commented:

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

Author Commented:
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

Author Commented:
oh i see why the .focus() is getting an error, its because of the (...) - what should be in there?
0

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

Author Commented:
So you aren't familiar with ASP.NET?
0

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

Author Commented:
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

Author Commented:
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

Commented:
all you've to do is ..

on submit button click .. validate the text box instead of validating on lost_focus.
0

Author Commented:
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

Author Commented:
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

Commented:
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
end if
end sub
%>
0

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

Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.