?
Solved

Adding Javascript - stopping at a comma.

Posted on 2012-09-04
3
Medium Priority
?
403 Views
Last Modified: 2012-09-04
We have code used throughout an app that comma-formats entries as they are typed and then in some cases, some of the values are stripped of their commas and used to create a sum in the form.   This works just fine in 8 of the 9 modules in the form.    But in one module, it will only add two of the three together if they are over 10,000.    So if I add 100,000 + 100,000 + 100,000 I get 201,000.   Doesn't matter what order I do them in.  And if I try to make one of them 0 I end up with a stray 1 in the ones column.   This doesn't happen anywhere else with the comma formatting piece.   No errors in javascript debug.    Any ideas what I am missing??

This is the code that is wonky:
function ExpCalc() {
		if (document.SurveyTakeForm.DQuestion_16132.value > 0 || document.SurveyTakeForm.DQuestion_16133.value > 0 || document.SurveyTakeForm.DQuestion_16134.value > 0) {
		document.SurveyTakeForm.DQuestion_16131.value = 0
		v16132 =  parseFloat(document.SurveyTakeForm.DQuestion_16132.value.replace(/,/g,""));
		v16132 = isNaN(v16132)?0:v16132;
		
		v16133 =  parseFloat(document.SurveyTakeForm.DQuestion_16133.value.replace(/,/g,""));
		v16133 = isNaN(v16133)?0:v16133;
		
		v16134 =  parseFloat(document.SurveyTakeForm.DQuestion_16134.value.replace(/,/g,""));
		v16134 = isNaN(v16134)?0:v16134;
		
		var ExpCalc = v16132 + v16133 + v16134;
		var ExpCalc = ExpCalc.toFixed(2);
		
		document.SurveyTakeForm.DQuestion_16131.value = CommaFormatted(ExpCalc);
	}
    }

Open in new window


and this is an exmple of the code that works just fine.

function ExpCalc() {
if (parseFloat(document.SurveyTakeForm.DQuestion_16105.value.replace(/,/g,"")) > 0 || parseFloat(document.SurveyTakeForm.DQuestion_16106.value.replace(/,/g,"")) > 0 || parseFloat(document.SurveyTakeForm.DQuestion_16107.value.replace(/,/g,"")) > 0) {
		document.SurveyTakeForm.DQuestion_16104.value = 0
		v16105 =  parseFloat(document.SurveyTakeForm.DQuestion_16105.value.replace(/,/g,""));
		v16105 = isNaN(v16105)?0:v16105;
		
		v16106 =  parseFloat(document.SurveyTakeForm.DQuestion_16106.value.replace(/,/g,""));
		v16106 = isNaN(v16106)?0:v16106;
		
		v16107 =  parseFloat(document.SurveyTakeForm.DQuestion_16107.value.replace(/,/g,""));
		v16107 = isNaN(v16107)?0:v16107;
		
		var ExpCalc = v16105 + v16106 + v16107;
		var ExpCalc = ExpCalc.toFixed(2);
		
		
		document.SurveyTakeForm.DQuestion_16104.value = CommaFormatted(ExpCalc);
	}
    }

Open in new window


This is the formatCurrency code:

function formatCurrency(element, __event)
   {
      if (oldValue[element.id] == undefined)
      {
         oldValue[element.id] = "";
      }
      var keyCode;
      if (window.event) // IE
      {
         keyCode = __event.keyCode;
      }
      else if (__event.which) // Netscape/Firefox/Opera
      {
         keyCode = __event.which;
      }
      var shiftDown = __event.shiftKey;
      var ctrlDown  = __event.ctrlKey;
      var altDown   = __event.altKey;
      var validChar = 
      (
         (
            (
               (
                  ((keyCode >= VK_0)       && (keyCode <= VK_9))       || 
                  ((keyCode >= VK_NUMPAD0) && (keyCode <= VK_NUMPAD9))
               ) || 
               (
                  (keyCode == VK_HYPHEN)   || 
                  (keyCode == VK_SUBTRACT)
               ) || 
               (
                  (keyCode == VK_PERIOD)  || 
                  (keyCode == VK_DECIMAL)
               ) || 
               (
                  (keyCode == VK_DELETE) || 
                  (keyCode == VK_BACK)
               )
            ) && !shiftDown && !ctrlDown && !altDown
         ) || 
         (
            (
               (keyCode == VK_HOME)  || 
               (keyCode == VK_END)   || 
               (keyCode == VK_LEFT)  || 
               (keyCode == VK_RIGHT)
            )
         ) && !ctrlDown && !altDown
      );
      if (validChar)
      {
         if ((keyCode == VK_HYPHEN) || (keyCode == VK_SUBTRACT))
         {
            if (element.value.indexOf("-") != 0)
            {
               element.value = oldValue[element.id];
            }
            return false;
         }
         else if ((keyCode == VK_PERIOD) || (keyCode == VK_DECIMAL))
         {
            if (element.value.indexOf(".") < (element.value.length - 1))
            {
               element.value = oldValue[element.id];
            }
            return false;
         }
         else if ((keyCode == VK_HOME) || (keyCode == VK_END) || (keyCode == VK_LEFT) || (keyCode == VK_RIGHT) || (keyCode == VK_DELETE))
         {
            return false;
         }
		 else if (keyCode == VK_BACK)
         {
         }
      }
      else
      {
         if ((keyCode != VK_SHIFT) && (keyCode != VK_CONTROL) && (keyCode != VK_MENU) && (keyCode != VK_TAB))
         {
            element.value = oldValue[element.id];
         }
         return false;
      }
      var number     = moneyToNumber(element.value);
      var negative   = ((""+number).substr(0, 1) == "-");
      number         = number.replace("-", "");
      var floatArray = number.split(".");
      floatArray[0]  = "" + parseFloat(floatArray[0]);
      var newValue   = addCommas(floatArray[0]);
      if (floatArray[1] != undefined)
      {
         newValue += "." + floatArray[1].substr(0, 2);
      }
      if (negative)
      {
         newValue = "-" + newValue;
      }
      if (element.value != newValue)
      {
         element.value        = newValue;
         oldValue[element.id] = newValue;
      }
      return false;
   }

Open in new window


and here is what the fields look like:

<input name="DQuestion_16133" type="text" value="0.00" maxlength="20"  size="15"  onkeyup="ExpCalc();formatCurrency(this, event);hiliteIt()"  id="DQuestion_16133"  /> 

Open in new window

0
Comment
Question by:digitalwise
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
Barry62 earned 2000 total points
ID: 38366445
didn't you leave off the parsefloat function from the wonky code?  It is there on the working code.
0
 

Author Comment

by:digitalwise
ID: 38366483
Sigh - said the blind man to the deaf man!   Too many things going on - duh!!!  Thank you.
0
 
LVL 8

Expert Comment

by:Barry62
ID: 38366488
You're welcome.  It happens to the best of us!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 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