Object Access By Reference?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • algifford
    New Member
    • Feb 2008
    • 3

    Object Access By Reference?

    OK...I have a function that formats some form input data. When I run the function I save the form object in an array...So far so good.

    Now when I am done with my page I want to go back and remove the formatting from the objects. Should be easy since I saved those objects previously, but it appears that what I have is a copy of those objects and not a reference to those objects. So anything I try to update at the end does nothing to the actual object. At least I do not see a change in the DOM. Is there a way to do this without having to crawl all over the DOM to update the values?

    [CODE=javascript]{
    var arDone = new Array;

    function formatCurrency( obj) {
    var num = (parseInt(strip Number(obj.valu e) * 100 + .5) / 100).toFixed(2) ;

    while(num.match (/\d{4}[\.\,]/)) {
    num = num.replace(/(\d+)(\d{3})/, '$1,$2');
    }

    obj.value = num;
    arDone.push(obj );

    return num;
    }

    function cleanCommas() {
    var obj;
    for (var i = 0; i < arDone.length; i++) {
    arDone.value = arDone[i].value.replace(/\,/g, '');

    alert(arDone.va lue);
    }
    alert(document. form1.mortgage1 _balance.value) ;
    return 1;
    }

    }[/CODE]
    Last edited by acoder; Feb 7 '08, 09:10 AM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Surely arDone.value should be arDone[i].value?

    Comment

    • algifford
      New Member
      • Feb 2008
      • 3

      #3
      Originally posted by acoder
      Surely arDone.value should be arDone[i].value?
      DOH!!!!!!!

      Never try to code after 48 hours without sleep.....Thank s...You are of course correct, and everything works just beautiful when you do that...

      Problem solved....

      In the interest of giving back to the community, here is the finished code...

      [CODE=javascript]{
      var arDone = new Array;

      function stripNumber(num ) {
      return num.toString(). replace(/[^\d\.-]/g, '');
      }

      function formatCurrency( obj) {
      var num = (parseInt(strip Number(obj.valu e) * 100 + .5) / 100).toFixed(2) ;

      while(num.match (/\d{4}[\.\,]/)) {
      num = num.replace(/(\d+)(\d{3})/, '$1,$2');
      }

      obj.value = num;
      arDone.push(obj );

      return num;
      }

      function cleanCommas() {
      for (var i = 0; i < arDone.length; i++) {
      arDone[i].value = arDone[i].value.replace(/\,/g, '');
      }

      return 1;
      }

      }[/CODE]
      Last edited by algifford; Feb 7 '08, 06:36 PM. Reason: Added Final Code

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Thanks for posting the solution. Post again if you have more questions.

        Comment

        • algifford
          New Member
          • Feb 2008
          • 3

          #5
          No prob...Happy to help, for those who missed it, The solution includes a killer money formatting algorithm...beh old the power of regular expressions...

          StripNumber removes all characters except for numbers, periods (for decimal point), and hyphens (for negative sign). This could be made more specific by removing any hyphens not on the front of the number or only allowing the first period, but this was good enough for what I was doing.

          FormatCurrency begins by stripping the number, then to accomodate 2 decimal place accuracy it multiplies by 100 and in order to round up it adds .5 and then drops anything after the decimal point. Next, it returns the 2 digits after the decimal by dividing by 100 and to make it display pretty, uses toFixed to add any trailing zeros to the output.

          The last step is to add commas in the thousands, millions, billions, etc. position. It does this by looking for 4 decimal digits in front of a comma or period. If it finds the four digits, it replaces them with whatever digits come first, followed by a comma and then the trailing 3 digits. It repeats this until you do not have four digits in a row.

          You could also include a preceeding dollar sign at this point if desired, or you could wrap the entire number in parentheses if it is negative and just throw the hyphen away. Your mileage may vary.

          Finally it assigns the formatted number back to the object.
          That's it! No ginsu knives or shipping and handling...Just clean simple code thank to the genius who invented regular expressions.

          If you want to thank me for this little gem, take a minute and help the next guy who comes along. What goes around comes around. The world is a better place if we all lend a hand.

          The following is the javascript code followed by a sample use in an html form:

          [CODE=javascript] function stripNumber(num ) {
          return num.toString(). replace(/[^\d\.-]/g, '');
          }

          function formatCurrency( obj) {
          var num = (parseInt(strip Number(obj.valu e) * 100 + .5) / 100).toFixed(2) ;

          while(num.match (/\d{4}[\.\,]/)) {
          num = num.replace(/(\d+)(\d{3})/, '$1,$2');
          }

          obj.value = num;
          } [/CODE]

          ----- HTML ------
          <input type="text" name="money" onChange="forma tCurrency(this) ;" />
          Last edited by acoder; Feb 12 '08, 07:52 AM.

          Comment

          Working...