17 Replies Latest reply on Dec 31, 2007 1:19 PM by nrutter

    Ignore dollar sign and commas

    nrutter Level 1
      Can someone please help me. I have two fields that do calculations based off of what a user inputs for a homes value. Basically I want to ignore dollar signs and commas if they put that in for the homes value. So if the user inputs $120,000.00 I still want the value to calculate even though it has the dollar sign and comma in it....Any ideas?




        • 1. Re: Ignore dollar sign and commas
          paulfeuer Level 1

          try

          var currencyValue:Number = Number( currencyString.replace(/[,$]/, "") );

          ./paul
          • 2. Re: Ignore dollar sign and commas
            nrutter Level 1
            I was not able to get that to work
            • 3. Re: Ignore dollar sign and commas
              paulfeuer Level 1
              sorry - i forgot the global flag

              var currencyValue:Number = Number( currencyString.replace(/[,$]/g, "") );

              here's an example:

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()">
              <mx:Script><![CDATA[
              private function init():void {
              var currencyString:String = "$120,000.00";
              var stripped:String = currencyString.replace(/[,$]/g, "");
              var currencyValue:Number = Number( stripped );
              output1.text = stripped;
              output2.text = String(currencyValue * 2);
              }
              ]]></mx:Script>
              <mx:Label id="output1" />
              <mx:Label id="output2" />
              </mx:Application>
              • 4. Re: Ignore dollar sign and commas
                nrutter Level 1
                I am not sure how to apply something like that to an onchange like I am doing. These are values that are being entered into a textInput so onchange this still needs to calculate. I understand how to do this with a value that is static.
                • 5. Re: Ignore dollar sign and commas
                  Garyl Woolworth Level 1
                  Why not just restrict the characters that are allowed within the textinput so that you don't have to compensate for the $ and ,'s

                  <mx:TextInput x="139" y="97" width="101" id="old_home" restrict="0-9." change="oldHomeChange()"/>
                  <mx:TextInput x="303" y="97" width="101" id="new_home" restrict="0-9." change="newHomeChange()"/>

                  This will allow them to only type 120000.00
                  • 6. Re: Ignore dollar sign and commas
                    paulfeuer Level 1
                    yah - and you could use a NumberValidator to restrict.

                    rnutter - there is no difference between a hard-coded string like that and a string property on one of your objects. here is a slightly more elaborate example:

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()"
                    width="200" height="200" horizontalScrollPolicy="off" verticalScrollPolicy="off">
                    <mx:Script><![CDATA[
                    import mx.formatters.CurrencyFormatter;
                    private var fmt:CurrencyFormatter = new CurrencyFormatter();
                    private function init():void {
                    input1.text="$120,000.00";
                    input2.text="$150,000.00";
                    fmt.precision = 2;
                    fmt.useThousandsSeparator = true;
                    onChange();
                    }
                    private function onChange(evt:Event=null):void {
                    var a:Number = Number( input1.text.replace(/[,$]/g, "") );
                    var b:Number = Number( input2.text.replace(/[,$]/g, "") );
                    sum.text = fmt.format(a+b);
                    }
                    ]]></mx:Script>
                    <mx:VBox width="100%" height="100%">
                    <mx:TextInput id="input1" restrict="$0-9,." change="onChange(event)" />
                    <mx:TextInput id="input2" restrict="$0-9,." change="onChange(event)" />
                    <mx:HBox width="100%"><mx:Label text="Sum:" /><mx:Label id="sum" /></mx:HBox>
                    </mx:VBox>
                    </mx:Application>
                    • 7. Re: Ignore dollar sign and commas
                      nrutter Level 1
                      I understand what you are trying to do with that but that's really not what I am looking for. I want them to be able to type this into whatever format they want so they can enter $120,000 it doesn't matter. But I need to replace those $ and , in maybe a hidden field or something like that so I can continue with my calulations. What my app does is they can enter a value and based on the value they enter it hits the db and looks for a min and max number that is between those values and based of of the number typed it will output their discount. It does this for the old home price and new home price and then adds the two values together to get the total. I don't care about putting it into dollar format after they type it in. I just need a way to strip those out of there so I can contine.

                      What I would like to do is with the onchange just output the $120,000 to another checkbox with those stripped out 120000
                      • 8. Re: Ignore dollar sign and commas
                        paulfeuer Level 1
                        did you run the example i pasted? sounds like it does what you're looking for in terms of allowing the user to input "$120,000" and then using it as a number.

                        if it doesn't do what you wanted (aside from all the calculations), i must not be understanding something then.

                        ./paul
                        • 9. Re: Ignore dollar sign and commas
                          nrutter Level 1
                          Yes I did try it and It is very close to what I need, but the problem is this. I want the user to be able to input 120,000.00 but the problem is that when it is doing the onChange it doesn't see that is being a "correct" value in the DB so it will not output the incentive price in my label. So what I am trying to do with your code is to have the user input the $120,000.00 when they type this in it will go into another textbox with the format I want as the 120000.00. Then I can just hide that field and have my RemoteCall base everything off of the hidden field. Is that possible? I sure appreciate all your help on this!

                          • 10. Re: Ignore dollar sign and commas
                            paulfeuer Level 1
                            i must still be missing something. why don't you just send a correctly formatted value to your call? when you're in onChange() you have access to the value both as a string and as a number. also - you shouldn't need a whole separate textbox to store a string you're just referring to internally.

                            ./paul
                            • 11. Ignore dollar sign and commas
                              nrutter Level 1
                              It isn't stored in the db as the "proper" format. I am new to flex so if I am completely off let me know. It is stored in my db like this 120000. What I am doing in my remote call is comparing my min and my max. If the value is between 120000 and 140000 the incentive is 500.00. So when it is typed in $120,000.00 it doesn't see that value in the database so it never outputs the 500.00 into my incentives label. I just need the $120,0000 to be stripped down to nothing so it can see that it matches my min - max values that are stored in the db. I don't want to limit them to not be able to put in $ or , that's why I thought it might just be easier to strip it down into a hidden field???
                              • 12. Re: Ignore dollar sign and commas
                                paulfeuer Level 1
                                was going to send a private message, but the window is like 20 columns by 6 rows - hardly big enough to type a response.

                                two things
                                - make sure you're storing the value as a numeric value so that numeric comparisons work
                                - input1.text.replace(/[,$]/g, "") replaces all commas and dollar signs with the empty string. in other words, it removes all commas and dollar signs. the return type of "replace" is a String. but you can convert it to a Number like i did in the example above if your RemoteObject cares about that.

                                i've never used RemoteObject, and i can't see how you're using it here, but i would look in the documentation for help on how to use it.

                                ./paul
                                • 13. Ignore dollar sign and commas
                                  nrutter Level 1
                                  I am really close its just that when the page loads it automatically throws a dollar sign in my new textInput called oldHide.

                                  • 14. Re: Ignore dollar sign and commas
                                    paulfeuer Level 1
                                    try not calling oldHomeChange in init()
                                    • 15. Re: Ignore dollar sign and commas
                                      nrutter Level 1
                                      I removed that from init and it throws it in as soon as I type something in the top input box
                                      • 16. Re: Ignore dollar sign and commas
                                        Garyl Woolworth Level 1
                                        This is more of a suggestion then it is a true answer but generally when something like this needs to be done for a database query you perform your "cleaning up" actions on the server side as well.

                                        So call your service via remote object and pass $120,000.00
                                        Have your service then do the cleaning up of removing the $'s & ,'s
                                        Do your select query using your new value that you received after cleaning up the passed argument.
                                        On your result of the remove object service do your after result handling with the data you needed from the database.

                                        The reason being that you would do the "cleaning up" on the server side is you want the clients machine to do as little work as possible when running your application. So a way of doing that is sending the workload to the service and having the servers hardware perform the work and send back a result to the clients machine. Replacing 2 characters out of a 9 character string isn't much work but using replace in regular expressions on a much larger string is a lot of work for a clients machine to go through.. Just my thoughts though.
                                        • 17. Re: Ignore dollar sign and commas
                                          nrutter Level 1
                                          I was able to get the $ sign removed

                                          fmt.currencySymbol=""

                                          Kaotic101 that is a good idea but I am returning these values in an arrayCollection so I am not really sending a response back to the server.