Advertisement

07.29.2008 at 11:17PM PDT, ID: 23606377
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

javascript format number and dollar

Asked by riskyricky1972 in JScript, JavaScript, Asynchronous Javascript and XML (AJAX)

I have attached working fine except when I tried to backspace the number format does not seem working. For example, first I put
25000 and it will automatically change to 25,000 that is what i expect, but when I backspace 1 e.g. the value of the input box will be 25,00
and it suppose to be 2,500, unfortuneately, it doesn't automatically change it. Any experts can take a look and fix it?
500 pts for completed codes. Thank youStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
<script>
 
var oldValue = [];
 
   var VK_BACK     = 0x08;      // BACKSPACE key
   var VK_TAB      = 0x09;      // TAB key 
   var VK_SHIFT    = 0x10;      // SHIFT key 
   var VK_CONTROL  = 0x11;      // CTRL key 
   var VK_MENU     = 0x12;      // ALT key 
   var VK_END      = 0x23;      // END key 
   var VK_HOME     = 0x24;      // HOME key 
   var VK_LEFT     = 0x25;      // LEFT ARROW key 
   var VK_RIGHT    = 0x27;      // RIGHT ARROW key 
   var VK_DELETE   = 0x2E;      // DEL key 
   var VK_0        = 0x30;      // 0 key
   var VK_1        = 0x31;      // 1 key
   var VK_2        = 0x32;      // 2 key
   var VK_3        = 0x33;      // 3 key
   var VK_4        = 0x34;      // 4 key
   var VK_5        = 0x35;      // 5 key
   var VK_6        = 0x36;      // 6 key
   var VK_7        = 0x37;      // 7 key
   var VK_8        = 0x38;      // 8 key
   var VK_9        = 0x39;      // 9 key
   var VK_NUMPAD0  = 0x60;      // Numeric keypad 0 key
   var VK_NUMPAD1  = 0x61;      // Numeric keypad 1 key
   var VK_NUMPAD2  = 0x62;      // Numeric keypad 2 key
   var VK_NUMPAD3  = 0x63;      // Numeric keypad 3 key
   var VK_NUMPAD4  = 0x64;      // Numeric keypad 4 key
   var VK_NUMPAD5  = 0x65;      // Numeric keypad 5 key
   var VK_NUMPAD6  = 0x66;      // Numeric keypad 6 key
   var VK_NUMPAD7  = 0x67;      // Numeric keypad 7 key
   var VK_NUMPAD8  = 0x68;      // Numeric keypad 8 key
   var VK_NUMPAD9  = 0x69;      // Numeric keypad 9 key
   var VK_SUBTRACT = 0x6D;      // Subtract key 
   var VK_DECIMAL  = 0x6E;      // Decimal key 
   var VK_HYPHEN   = 0xBD;      // Windows 2000/XP: For any country/region, the '-_' key
   var VK_PERIOD   = 0xBE;      // Windows 2000/XP: For any country/region, the '.>' key
 
   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) || (keyCode == VK_BACK))
         {
            return false;
         }
      }
      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;
   }
 
   function addCommas(number)
   {
      return ((number.length > 3) ? (addCommas(number.substring(0, number.length - 3)) + "," + number.substring(number.length - 3, number.length)) : 
         String(number));
   }
 
   function moneyToNumber(money)
   {
      var number = money.replace("$", "");
      while (number.indexOf(",") > -1)
      {
         number = number.replace(",", "");
      }
      return number;
   }
</script>
htmlwrite = htmlwrite & "<tr><td><span id='originalcost'>Purchased Price $</span></td><td><input 		  type=""text""            class=""fullsize_textbox""     name=""ii_originalcost""      onkeyup=""formatCurrency(this, event)""  value=""" & ii_originalcost    & """></td></tr>"
[+][-]07.30.2008 at 01:30AM PDT, ID: 22118376

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: JScript, JavaScript, Asynchronous Javascript and XML (AJAX)
Sign Up Now!
Solution Provided By: mrdany
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628