Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Money Validator

Posted on 2004-04-19
Medium Priority
485 Views
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
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
• 9
• 7

LVL 9

Expert Comment

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

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

LVL 9

Expert Comment

ID: 10871912

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

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

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

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

ID: 10872018
So you aren't familiar with ASP.NET?
0

LVL 9

Expert Comment

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

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

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

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

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

LVL 2

Author Comment

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

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

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

LVL 9

Expert Comment

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

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

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

## Featured Post

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is moâ€¦
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â€¦
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on tâ€¦
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastrâ€¦
###### Suggested Courses
Course of the Month8 days, 11 hours left to enroll